For every matrix row find rows within the same matrix that have more than one common element in same index below the row
이전 댓글 표시
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
Walter Roberson
2019년 4월 27일
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
2019년 4월 27일
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
2019년 4월 27일
채택된 답변
추가 답변 (1개)
Walter Roberson
2019년 4월 27일
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)
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!