matlab: find the index of common values at the same entry from two arrays

血红的双手。 提交于 2020-01-04 13:44:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!