Find matrix rows that have 3 common values, store the rows and the values

2 ビュー (過去 30 日間)
Pseudoscientist
Pseudoscientist 2019 年 4 月 16 日
編集済み: Matt J 2019 年 4 月 17 日
Suppose I have a 500000x5 matrix and for each row I need to find rows that have 3 common elements within the first 4 elements of the row but different 5th element and store the common values and the sum of the 5th elements to a new matrix.
For example:
1 2 3 4 1
1 2 3 5 2
Would yield a row like this in a new matrix
1 2 3 3
Of course this could be done with 2 nested for loops and if statements but the calculation takes like 10 hours. I need something that takes a few minutes even with 500k rows.. Is this possible with Matlab?
  5 件のコメント
Pseudoscientist
Pseudoscientist 2019 年 4 月 16 日
Yes the following result is correct, displayig the result in ascending order is very good
Matt J
Matt J 2019 年 4 月 16 日
OK, see my answer then.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2019 年 4 月 16 日
編集済み: Matt J 2019 年 4 月 17 日
I'll call your original 5 column matrix A.
D=cell(4,1);
a5=A(:,5);
As=sort(A(:,1:4),2);
for i=1:4 %small loop
Triangles{i}= [As(:, setdiff(1:4,i)),a5];
end
Q=cell2mat(Triangles);
result=consol(Q(:,1:3),Q(:,4));
function out=consol(B,b)
[C,~,j]=unique(B,'rows');
nc=size(C,1);
c=accumarray(j,b,[nc,1]);
out=[C,c];
keep=histcounts(j,1:nc+1)>1;
out=out(keep,:);
end
  9 件のコメント
Pseudoscientist
Pseudoscientist 2019 年 4 月 17 日
編集済み: Matt J 2019 年 4 月 17 日
Hmm quite interesting, the logic seems to work but when the results are plotted, the result is not that great..
Imagine a sphere filled with a tetrahedral mesh, the tetras have values 1 and 2. Now where the tetras face each other, we should have a triangular surface of values 3.
The first image is what should be. The second image is what is calculated as triangles with values 3. The third image is the sphere.
The first image has about 43k triangles and
the second image
tri_3 = find(result(:,4)==4);
result1 = result(tri_3,:);
has about 3k
Matt J
Matt J 2019 年 4 月 17 日
編集済み: Matt J 2019 年 4 月 17 日
OK. One more version (I think this is it).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by