How to find an element in a row, corresponding to an element (in same row number) in another matrix?

1 次查看(过去 30 天)
Hello,
I am trying to find the location of an element (in terms of column number only) in a row (Let us assume this matrix to be A), corresponding to an element which exists in another matrix(Let us assume this matrix to be B).
Dimension of A=[1000X5]
Dimension of B=[1000X1]
MWE
A=[1,2,3,2,5;6,7,8,9,10;11,12,13,14,15];
B=[2;9;11];
(The answer is supposed to be [2;4;1])
Now I want to find the location (in terms of column number only) of 2, 9 and 11 (from matrix B) in row1,row2 and row3 (of matrix A)? Row1 (in A) contains two 2's therefore I want to choose the first location of 2 i.e. 2. Any pointers for this problem?

回答(2 个)

SuperNano
SuperNano 2013-5-12
You can use the find function. If you give it two variables as outputs it will return the corresponding column and row indices of all instances in the matrix where it finds the value you're after. Hope this helps.

Abhay
Abhay 2013-5-12
I hope this is useful to you.
for i=1:length(B) [r,c]=find(A==B(i)) end

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by