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

Home » Public Forums » archive » majority voting
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: majority voting [message #65158 is a reply to message #65079] Thu, 12 February 2009 04:17 Go to previous messageGo to previous message
Allan Whiteford is currently offline  Allan Whiteford
Messages: 117
Registered: June 2006
Senior Member
mort canty wrote:
> Hi all,
>
> Given a 2-D array such as
>
> 0 1 1 2 1
> 0 2 1 1 1
> 1 0 2 2 1
>
> where the entries are labels, the columns represent items and the rows
> are voters, I want a IDL function that returns the majority vote labels.
> So here I should get
>
> 0 ? 1 2 1
>
> as output, where ? = "don't care". There must _not_ be a loop over
> columns. I've got a clumsy solution, but I'm sure there's an elegant one
> somewhere?
>
> Cheers,
>
> Mort

Hi Mort,

It might be less efficient than JD's histogram solution (I didn't check)
but the following also fits the problem specification:

x=[ [0,1,1,2,1],$
[0,2,1,1,1],$
[1,0,2,2,1]]

voters=(size(x,/dim))[1]
items=(size(x,/dim))[0]
max_label=max(x)+1

f=intarr(max_label,items)
++f[max_label*(indgen(voters*items) / voters)+ $
reform(transpose(x),voters*items)]
junk=max(f,idx,dim=1)
print,idx - max_label*findgen(items)

Note that the above solution will also blow up when you end up with
sparse arrays (e.g. if you have someone voting for label 1000000 then f
will end up being an items x 1000000 array even if nobody votes for any
labels between 3 and 1000000).

I think all the discussions on finding the mode (either in 1D or nD)
probably pre-dated the ++ operator. It could be that using the
vectorised ++ operator is a better way to do it - I doubt it though,
normally if histogram can do something then histogram will be the best
way! You'd also need to introduce a clumsy offset to deal with negative
selections (Not an issue for you here but would be if finding the mode
in a more general way).

It would make David's 1D example from his webpage into something like this:

array = [1, 1, 2 , 4, 1, 3, 3, 2, 4, 5, 3, 2, 2, 1, 2, 6,-3]
f=intarr(max(array)-min(array)+1)
f[array-min(array)]++
junk=max(f,idx)
mode=idx + min(array)
print,mode

again, with no idea on what would be more efficient. If you're doing
analysis on measurements (typically non-integers) then you'd need to
invoke histogram anyway to bin them before trying to find the mode.

Thanks,

Allan
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: Reading multiple ASCII files in as 2d arrays and putting them into a 3d array
Next Topic: Different issue

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

Current Time: Fri Oct 10 02:00:57 PDT 2025

Total time taken to generate the page: 0.95481 seconds