comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: MODE in IDL?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: MODE in IDL? [message #47192 is a reply to message #47105] Wed, 25 January 2006 09:14 Go to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Tue, 24 Jan 2006 20:04:13 -0700, David Fanning wrote:


> array = [1, 1, 2 , 4, 1, 3, 3, 2, 4, 5, 3, 2, 2, 1, 2, 6]
> h = Histogram(array, MIN=Min(array))
> bigfreq = Max(h)
> mode = Where(h EQ bigfreq) + Min(array)
> Print, mode
> 2

Just a hint on your HISTOGRAM usage... this might be slightly preferred,
since it skips the WHERE and MIN:

array = [1, 1, 2 , 4, 1, 3, 3, 2, 4, 5, 3, 2, 2, 1, 2, 6]
void=max(histogram(array,OMIN=mn),mxpos)
mode=mn+mxpos

This method of course will be *very* problematic if you have, e.g.:

array = [1, 1, 2 , 4, 1, 3, 3, 2, 4, 5, 3, 2, 2, 1, 2, 6, 10000000]

Another option, if you worry about this, would be to re-cast as a
sorting problem, using the method discussed in a recent thread:

array=array[sort(array)]
wh=where(array ne shift(array,-1),cnt)
if cnt eq 0 then mode=array[0] else begin
void=max(wh-[-1,wh],mxpos)
mode=array[wh[mxpos]]
endelse
print,mode

Both methods will give you the lowest number in the case of ties for
the mode. The second will be slower, but more robust against large
dynamic range in your array. You could use both, deciding which to
use by the min/max of the array.

JD
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: IDL Quiz and Essay Contest
Next Topic: Reading DICOM images

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 20:04:16 PDT 2025

Total time taken to generate the page: 0.00545 seconds