For every matrix row find rows within the same matrix that have more than one common element in same index below the row
Show older comments
Let's say I have matrix A, every row contains three values and for every row of the matrix A need to find other rows that have more than one common element in same index below the present row. For example for row i, we are interested in rows i+1:end. Matrix A is sorted. If for a row there's no rows found that fulfil this condition the result cell should have value 0 in that row number.
A = [
1 2 3
1 4 5
3 4 5
1 2 4
1 2 5
2 4 5]
Result =
4,5
3,5,6
6
5
0
I'm looknig for a fast way to do that, the fastest I've done takes 90 minutes with 230k rows
3 Comments
Walter Roberson
on 27 Apr 2019
That output is not possible, as numeric arrays cannot be "ragged". You would either need a rectangular numeric array with padding, or else a cell array as output.
Walter Roberson
on 27 Apr 2019
Why is the output for the second row not 3,5,6, since [1 4 5] matches [1 2 5] at both 1 and 5 ? Why is the output for the 4th row not 5, since [1 2 4] matches [1 2 5] at both 1 and 2 ?
It looks to me as if you have a secret additional condition, that once a row below has matched, it is removed from further consideration.
Pseudoscientist
on 27 Apr 2019
Accepted Answer
More Answers (1)
Walter Roberson
on 27 Apr 2019
t1 = tril(A(:,1) == A(:,1).', -1);
t2 = tril(A(:,2) == A(:,2).', -1);
t3 = tril(A(:,3) == A(:,3).', -1);
mask = t1+t2+t3 > 1;
Then extract the positions from this. For example,
sort( mask .* (1:size(t1,1).', 'descend')
would give an array in which the columns contain the indices per row, zero padded at the end. Or you could
arrayfun(@(IDX) find(mask(:,IDX)), 1:size(mask,2), 'uniform', 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!