Finding matched rows of matrix with other matrix?

조회 수: 3 (최근 30일)
Ahmed Anas
Ahmed Anas 2020년 6월 24일
댓글: Ahmed Anas 2020년 6월 25일
Lets say we have two matrix.
A=[1 1]
B=[2 0;1 1;1 1;3 1]
Now second and third rows of B are same as matrix A. and I want to find indices of such rows. i.e 2 and 3
I have tried 'ismember' but it is giving just first matched row index i.e. 2 (code can be seen below)
Thanks
A=[1 1]
B=[2 0;1 1;1 1;3 1]
[L, M] = ismember(A,B, 'rows')

채택된 답변

Gaganjyoti Baishya
Gaganjyoti Baishya 2020년 6월 24일
arr=[];
for i=1:size(B,1)
c=0;
for j=1:2
if B(i,j)==A(1,j)
c=c+1;
end
end
if c==2
arr=[arr i]
end
end
arr gives your required indices
  댓글 수: 1
Ahmed Anas
Ahmed Anas 2020년 6월 25일
Thanks ,Gaganjyoti Baishya.
But i have also developed the code below:
A=[1 2];
B=[1 1;1 2;3 2;2 1;1 2;1 2];
Index=1;
for i=1:size(B,1)
Ismatched=ismember(A,B(i,:),'rows');
if Ismatched==1
MatchedRows(Index)=i;
Index=Index+1;
end
end
MatchedRows

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by