Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,571
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Quote Originally Posted by 007julien View Post
    There is never two dominant numbers !
    Well, true, if the definition of dominant is "more than half the length of the array". But I was just following the lead of your original code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    New to the CF scene
    Join Date
    Dec 2012
    Location
    London
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, exactly this. Much better way than mine.

  3. #18
    New Coder donna1's Avatar
    Join Date
    Nov 2012
    Location
    london
    Posts
    99
    Thanks
    9
    Thanked 4 Times in 4 Posts
    why do you keep talking about more than half?
    as i pointed out earlier the Mode isnt always as common as half the elements

    Dominant number is not actually a known mathematical term so i assume he ment mode?
    Last edited by donna1; 12-02-2012 at 03:34 AM.

  4. #19
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by donna1 View Post
    why do you keep talking about more than half?
    as i pointed out earlier the Mode isnt always as common as half the elements

    Dominant number is not actually a known mathematical term so i assume he ment mode?
    Not so.

    http://stackoverflow.com/questions/9...er-in-an-array

    A real number in the array is called a decimal dominant if it occurs more than n/10 times in the array.

    But I am not sure what the practical use of this.
    Last edited by Philip M; 12-02-2012 at 12:43 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  5. #20
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    This kind of script can be useful to study relative frequencies of letters in the English language... Here is an example.

    The issue is: The frequency variations can they give an indication of the author's speech?

  6. #21
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,401
    Thanks
    11
    Thanked 595 Times in 575 Posts
    is there anything map/filter can't do?

    Code:
    r=[ 77, 101, 191, 91, 91, 191, 191, 191, 191,343 ]
      .map(function(a){return this[a]?(this[a]+=1):(this[a]=1),this;},[])[0]
    
    r.indexOf(Math.max.apply(0,r.filter(Number))); // === 191
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  7. #22
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by rnd me View Post
    is there anything map/filter can't do?

    Code:
    r=[ 77, 101, 191, 91, 91, 191, 191, 191, 191,343 ]
      .map(function(a){return this[a]?(this[a]+=1):(this[a]=1),this;},[])[0]
    
    r.indexOf(Math.max.apply(0,r.filter(Number))); // === 191
    Yes - report the mode when two values occur the same number of times. Obviously you can have more than one mode.
    Having two modes is called "bimodal". More than 2 modes is called "multi-modal".

    felgall's and Old Pedant's scripts do this.
    Last edited by Philip M; 12-03-2012 at 07:55 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  8. #23
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,401
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by Philip M View Post
    Yes - report the mode when two values occur the same number of times. Obviously you can have more than one mode.
    Having two modes is called "bimodal". More than 2 modes is called "multi-modal".

    felgall's and Old Pedant's scripts do this.
    my bad, i didn't understand that was required. i've never been good at math...

    that does make a bit more work.
    what a good opportunity to expand my functional example!

    to compare a collection, i needed a variable (counts), and a way get the unique values, unique. i went ahead and named count() and pulled it out of the body to reduce clutter.

    isn't it neat how those new pieces bolt right on to the existing solution? that's functional programming for ya.
    Code:
    function unique(a){return this[a]?0:(this[a]=1)}
    function count(a){ return this[a]?(this[a]+=1):(this[a]=1),this;}
    
    r=[ 77, 101, 141, 91, 91,  191, 191,343 ]
    
    counts=r.map( count, [])[0];
    
    r.filter(function(a,b){ 
       return counts[a]==this;
      },   
       Math.max.apply(0, counts.filter(Number))
    ).filter( unique, {});
    this should output 91 and 191, right? it does.

    and given[ 77, 101, 141, 91, 91, 191, 191,343, 44,44,65 ], it gives [91, 191, 44], so it's appears to be a scale-able solution.
    Last edited by rnd me; 12-03-2012 at 08:47 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  9. #24
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by rnd me View Post
    this should output 91 and 191, right? it does.

    and given[ 77, 101, 141, 91, 91, 191, 191, 343, 44, 44, 65 ], it gives [91, 191, 44], so it's appears to be a scale-able solution.
    Yep, it does!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  10. #25
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Thanks ! is it very necessary to return many values with map ?

    Code:
    var r=[ 77, 101, 141, 91, 91, 191, 191, 343, 44, 44, 65 ],lttFrq=[];// an array or an object
       function count(a){lttFrq[a]?lttFrq[a]+=1:(lttFrq[a]=1);}
       r.map(count);
       alert(lttFrq);
    Then (for IE only9 web users) filter give the «dominant» value, or all others frequencies...

  11. #26
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,401
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by 007julien View Post
    Thanks ! is it very necessary to return many values with map ?
    no, i returned an array to avoid a variable.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  12. #27
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,571
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Of course, the original poster *did* make the requirement that, *FOR HIM*, "dominant" implied that the number had to appear more than half the time.

    Qoute from post #3:
    Yes exactly. I mean more times than the half
    So all the stuff I did, and RndMe did, to produce multiple answers in the case of duplicates is fun but not, per his statement, needed.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •