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

5 views (last 30 days)
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?

Answers (2)

SuperNano
SuperNano on 12 May 2013
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 on 12 May 2013
I hope this is useful to you.
for i=1:length(B) [r,c]=find(A==B(i)) end

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!