Enjoy an ad free experience by logging in. Not a member yet?
Register .
12-01-2012, 11:50 PM
PM User |
#16
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,185
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Quote:
Originally Posted by
007julien
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.
12-01-2012, 11:53 PM
PM User |
#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.
12-02-2012, 03:32 AM
PM User |
#18
New Coder
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 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?
Last edited by donna1; 12-02-2012 at 03:34 AM ..
12-02-2012, 12:37 PM
PM User |
#19
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Quote:
Originally Posted by
donna1
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.
__________________
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.
Last edited by Philip M; 12-02-2012 at 12:43 PM ..
12-02-2012, 02:59 PM
PM User |
#20
Regular Coder
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 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?
12-03-2012, 07:24 AM
PM User |
#21
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,454
Thanks: 9
Thanked 466 Times in 450 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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
12-03-2012, 07:43 AM
PM User |
#22
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Quote:
Originally Posted by
rnd me
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.
__________________
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.
Last edited by Philip M; 12-03-2012 at 07:55 AM ..
12-03-2012, 08:43 AM
PM User |
#23
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,454
Thanks: 9
Thanked 466 Times in 450 Posts
Quote:
Originally Posted by
Philip M
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
Last edited by rnd me; 12-03-2012 at 08:47 AM ..
12-03-2012, 09:43 AM
PM User |
#24
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Quote:
Originally Posted by
rnd me
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.
12-03-2012, 01:56 PM
PM User |
#25
Regular Coder
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 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...
12-03-2012, 06:28 PM
PM User |
#26
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,454
Thanks: 9
Thanked 466 Times in 450 Posts
Quote:
Originally Posted by
007julien
Thanks ! is it very necessary to return many values with map ?
no, i returned an array to avoid a variable.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
12-04-2012, 01:06 AM
PM User |
#27
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,185
Thanks: 59
Thanked 3,995 Times in 3,964 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:
Quote:
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.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 03:22 AM .
Advertisement
Log in to turn off these ads.