Clear Filters
Clear Filters

Compare 2 dimensional matrix for same pair elements

5 views (last 30 days)
I want to compare 2 two-dimensional matrices for the identical pair elements. For example, A = [1,1,2,3,3,4,5,6,6;2,3,1,4,5,1,2,3,5] and B = [1,2,4,3,3,5;2,1,2,5,4,2], so, it should return that elements [1,2,3,5;2,1,5,2] are the ones which also exists in the matrix A.
a = [1,1,2,3,3,4,5,6,6;2,3,1,4,5,1,2,3,5];
b = [1,2,4,3,3,5;2,1,2,5,4,2];
for i = 1:length(a)
for j = 1:length(b)
if b(1:(j))==a(1,(i)) && b(2,(j))==a(2,(i))
%*do something*
end
end
end

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 17 Mar 2016
a = [1,1,2,3,3,4,5,6,6;2,3,1,4,5,1,2,3,5];
b = [1,2,4,3,3,5;2,1,2,5,4,2];
aa=a.';
bb=b.';
index=ismember(aa,bb,'rows');
out=aa(index,:);
out=out.'
out =
1 2 3 3 5
2 1 4 5 2

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices 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!