The solution offered by Acorbe can be quite slow if you have many lines and / or long lines. I checked that in most cases the two solutions that I cited below should be significantly faster. If your matrix contains only a few different values (relative to the size of the matrix), then this should work pretty quickly:
function rowMatches = find_row_matches(A) % Returns a cell array with row matches for each row c = unique(A); matches = false(size(A,1), numel(c)); for i = 1:numel(c) matches(:, i) = any(A == c(i), 2); end rowMatches = arrayfun(@(j) ... find(any(matches(:, matches(j,:)),2)), 1:size(A,1), 'UniformOutput', false);
This alternative may be faster if you have short lines, i.e. when size(A,2) small:
function answers = find_answers(A) % Returns an "answers" matrix like in Acorbe solution c = unique(A); answers = false(size(A,1), size(A,1)); idx = 1:size(A,1); for i = 1:numel(c) I = any(A == c(i), 2); uMatch = idx(I); answers(uMatch, uMatch) = true; isReady = all(A <= c(i), 2); if any(isReady), idx(isReady) = []; A = A(~isReady,:); end end
source share