One possible solution:
function [Index] = GetIntersectionIndicies(m1, m2) [~, I1] = intersect(m1, m2, 'rows'); Index = zeros(size(m1, 1), 1); Index(I1) = 1;
By the way, I like @Shai's inventive solution, and it is much faster than my solution if your matrices are small. But if your matrices are large, then my decision will dominate. This is due to the fact that if we set T = size(m1, 1) , then the tmp variable in @Shai's answer will be T * T, i.e. Very large matrix if T is large. Here is some code for a quick speed test:
%# Set parameters T = 1000; M = 10; %# Build test matrices m1 = randi(5, T, 2); m2 = randi(5, T, 2); %# My solution tic for m = 1:M [~, I1] = intersect(m1, m2, 'rows'); Index = zeros(size(m1, 1), 1); Index(I1) = 1; end toc %# @Shai solution tic for m = 1:M tmp = bsxfun( @eq, permute( m1, [ 1 3 2 ] ), permute( m2, [ 3 1 2 ] ) ); tmp = all( tmp, 3 ); % tmp(i,j) is true iff m1(i,:) == m2(j,:) imdices = any( tmp, 2 ); end toc
Set T = 10 and M = 1000 , and we get:
Elapsed time is 0.404726 seconds. %# My solution Elapsed time is 0.017669 seconds. %# @Shai solution
But set T = 1000 and M = 100 and get:
Elapsed time is 0.068831 seconds. %# My solution Elapsed time is 0.508370 seconds. %# @Shai solution