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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by