X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 20 Jun 2007 12:31 PM by  anon
quick find array index, eliminate the loop
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
20 Jun 2007 12:31 PM
    hello, I have two arrays: a=[0,1,2,3,4,5,6,7,8,9] and b=[1,3,5,7,9]. How could I get the index of a wihch value equal b? And 'index = where(a eq b)' doesn't work. Is there a way to do this and eliminate the loop? Regards, Dong

    Deleted User



    New Member


    Posts:
    New Member


    --
    20 Jun 2007 12:31 PM
    With two arrays of unequal length the IDL language writers had to make a decision about what the meaning of "WHERE(arrayOfOneLength EQ arrayOfDifferentLength)" should mean. The logical option was to choose the meaning closest to the behavior of WHERE with two arrays of equal length: the output of WHERE would be the subscripts where array1[i] EQ array2[i]. So, if your example were: a = [1, 4, 9, 17, 22, 25] b = [1, 22, 25, 17, 9, 4] then the output of "WHERE(a eq b)" would be [0, 3]. Since IDL has set WHERE to work this way, it does not have the option to also work the way you want. And there is no one-call alternative to WHERE that does the kind of INTERSECTION functionality that you would like in your example. Thus, in this case you have to make a specific for loop. Here is a routine that might be appropriate for your application. FUNCTION intersection, set, subset intersection = bytarr(n_elements(set)) ; Initialize boolean result array for i = 0, n_elements(subset)-1 do begin trueIndices = where(set eq subset[i], count) if count ne 0 then intersection[trueIndices] = 1B endfor trueIndices = where(intersection eq 1B, count) if count ne 0 then return, trueIndices else return, -1 END James Jones
    You are not authorized to post a reply.