My initial idea is below, but someone may be able to improve on it in efficiency or elegance.
There is an awkwardness in this solution: if row A has two elements in common with both rows B and C, then the results may contain both [A B C] and [B C]. There are various things you could do about this - but a more precise specification of the problem is needed in order to choose how to handle such cases. If the data only ever has pairs of matching rows, there's no problem.
a = [5 6 2;
3 2 7;
4 6 5;
9 7 2;
5 3 8]
ap = a.';
groups = cell(1, size(ap,2)-1);
ngroups = 0;
for k = 1:size(ap, 2)-1
same = bsxfun(@ismember, ap(:,k), ap(:,k+1:end));
matches = find(sum(same) >= 2);
if ~isempty(matches)
ngroups = ngroups+1;
groups{ngroups} = [k k+matches];
end
end
groups = groups(1:ngroups);
Best Answer