Comparing two matrices to find common elements

조회 수: 13 (최근 30일)
Srijeet Tripathy
Srijeet Tripathy 2018년 7월 10일
댓글: Srijeet Tripathy 2018년 7월 10일
Dear Users, I have two matrices where one is the subset of the other. I want to compare both the matrices row wise and I want to generate a column vector that contains the list of rows (row number) that comprise the smaller matrix. I have written a small code but it is not doing the job. Please have a look.
if true
% code
end
sizesmallermatrix = size(smallermatrix);
sizelargermatrix = size(largermatrix);
compare = [];
wc = 0;
for i = 1:sizesmallermatrix(1)
xA = smallermatrix(i, 1);
yA = smallermatrix(i, 2);
xB = smallermatrix(i, 3);
yB = smallermatrix(i, 4);
for j = i+1:sizelargermatrix(1)
xC = largermatrix(j, 1);
yC = largermatrix(j, 2);
xD = largermatrix(j, 3);
yD = largermatrix(j, 4);
wc = wc+1;
if (xA == xC)&&(yA == yC)&&(xB == xD)&&(yB == yD)
compare(count, 1) = j;
end
end
end
  댓글 수: 2
jonas
jonas 2018년 7월 10일
Same amount of columns in both matrices?
Srijeet Tripathy
Srijeet Tripathy 2018년 7월 10일
That's right!

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

채택된 답변

Guillaume
Guillaume 2018년 7월 10일
Simply use ismember:
[rowisinlarger, where] = ismember(smallermatrix, largermatrix, 'rows')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by