MATLAB: Comparing two matrices to find common elements

for loopmatrixnested loop

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

Best Answer

Simply use ismember:
[rowisinlarger, where] = ismember(smallermatrix, largermatrix, 'rows')