histogram & reverse_indices [message #30071] |
Tue, 09 April 2002 12:15  |
Ken Mankoff
Messages: 158 Registered: February 2000
|
Senior Member |
|
|
Hi,
As you may have guessed from the subject, I have a question about
histogram and reverse indices...
I have a 2D array made up of n quadruplets. Ex:
array =[[1,1,1,2], $
[1,1,1,1], $
[3,4,3,2], $
[3,3,0,0], $
[5,5,0,5]]
I want my algorithm to do the following: Return the index of all the
quadruplets that have at least 3 out of 4 numbers equal to each other
(i.e. for the above array, it should return [0,1,4].
I can do it in a for-loop as follows:
for i=0,n_elements(array[0,*])-1 do begin
quad = array[*,i]
hist = histogram( quad )
hist = hist[ where( hist ne 0 ) ]
if ( max( hist ) gt 3 then print, 'good' else print, 'bad'
endfor
But I think there is a way to do this without a for loop. Either using
reverse_indices, or where(), I just cannot see it. Can you?
-k.
--
Kenneth Mankoff
LASP://303.492.3264
http://lasp.colorado.edu/~mankoff/
http://lasp.colorado.edu/snoe/
http://lasp.colorado.edu/mars/
http://lasp.colorado.edu/marsrobot/
|
|
|
Re: histogram & reverse_indices [message #30211 is a reply to message #30071] |
Wed, 10 April 2002 07:26  |
Ken Mankoff
Messages: 158 Registered: February 2000
|
Senior Member |
|
|
On 9 Apr 2002, Craig Markwardt wrote:
> Wayne Landsman <landsman@mpb.gsfc.nasa.gov> writes:
>
>> Ken Mankoff wrote:
>>
> ...
>>> I want my algorithm to do the following: Return the index of all the
>>> quadruplets that have at least 3 out of 4 numbers equal to each other
>>> (i.e. for the above array, it should return [0,1,4]
>>
>> Here's a non-loop solution for the specific case, although it is a
>> solution that is difficult to generalize, and which may be less
>> understandable and slower than simply using a loop.
>
> Ooof, Wayne beat me to the punch. It looks like a good technique.
>
Yep, that works. Thanks!
-k.
|
|
|
|
Re: histogram & reverse_indices [message #30216 is a reply to message #30071] |
Tue, 09 April 2002 19:35  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Wayne Landsman <landsman@mpb.gsfc.nasa.gov> writes:
> Ken Mankoff wrote:
>
...
>> I want my algorithm to do the following: Return the index of all the
>> quadruplets that have at least 3 out of 4 numbers equal to each other
>> (i.e. for the above array, it should return [0,1,4]
>
> Here's a non-loop solution for the specific case, although it is a
> solution that is difficult to generalize, and which may be less
> understandable and slower than simply using a loop.
Ooof, Wayne beat me to the punch. It looks like a good technique.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: histogram & reverse_indices [message #30220 is a reply to message #30071] |
Tue, 09 April 2002 13:47  |
Wayne Landsman
Messages: 117 Registered: January 1997
|
Senior Member |
|
|
Ken Mankoff wrote:
>
> I have a 2D array made up of n quadruplets. Ex:
> array =[[1,1,1,2], $
> [1,1,1,1], $
> [3,4,3,2], $
> [3,3,0,0], $
> [5,5,0,5]]
>
> I want my algorithm to do the following: Return the index of all the
> quadruplets that have at least 3 out of 4 numbers equal to each other
> (i.e. for the above array, it should return [0,1,4]
Here's a non-loop solution for the specific case, although it is a
solution that is difficult to generalize, and which may be less
understandable and slower than simply using a loop.
The idea is that if 3 out of 4 numbers are equal to each other, then that
number is either the minimum or the maximum of the quadruplet. So we
first get the min and max of each quadruplet.
amin = min(array,dimen=1,max=amax) ;V5.5 needed
Now reform/rebin the min and max vectors into a 2d arrays
amax = rebin(reform(amax,1,5),4,5)
amin = rebin(reform(amin,1,5),4,5)
Now find which values in the array are equal to either the minimum or the
maximum. Total along rows
to determine if 3 or more values in a quadruplet meet this condition:
print,where( (total((array EQ amin),1) GE 3) or $
(total((array EQ amax),1) GE 3))
---> [0,1,4]
--Wayne
landsman@mpb.gsfc.nasa.gov
|
|
|