问题
Suppose that I have two arrays of size p (p is large) that have values from 0,1,2. I am looking for a way to find the index that both arrays have value 2 at the same entry, the index that one has value 2 and the other one has value 1, etc (like 2 and 2, 2 and 1, `2 and0,1and1,1and0`).
Is there a way to achieve this without using the for loop on p?
回答1:
You can use logical arrays, let A and B be your arrays (with consistent sizes):
Indices=find((A==2)&(B==2));
Etc. for other operations.
Note that if A and B have dimensions higher than 1, Indices will be a linear index, change to [x,y]=find((A==2)&(B==2)); if you want row/column pairs.
来源:https://stackoverflow.com/questions/21409275/matlab-find-the-index-of-common-values-at-the-same-entry-from-two-arrays