How to find an element in a row, corresponding to an element (in same row number) in another matrix?
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
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.
0 Comments
Abhay
on 12 May 2013
I hope this is useful to you.
for i=1:length(B) [r,c]=find(A==B(i)) end
0 Comments
See Also
Categories
Find more on Logical 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!