フィルターのクリア

Addition of identical matrix sections

1 回表示 (過去 30 日間)
Tom Engels
Tom Engels 2021 年 8 月 2 日
コメント済み: Tom Engels 2021 年 8 月 2 日
Hello all,
I have the following problem:
I have a nx6 matrix with floating point numbers. Now I want to take the first three columns and see if there are duplicates. The duplicates found are then to be combined with each other. The data of the first three columns should not change and only columns 4 to 6 should be added up. The duplicates of the first three columns are then deleted.
Here is an example:
A = [1 2 3 4 5 6; ...
2 3 4 2 3 1; ...
2 4 2 2 2 2; ...
1 2 3 1 1 1];
--> Reduce
A_new = [1 2 3 5 6 7; ...
2 3 4 2 3 1; ...
2 4 2 2 2 2];
Unfortunately, I could not achieve success with splitapply and accumarray. Is there another Matlab function that can do this or do I have to handle this with an if statement?
Regards
Tom

採用された回答

KSSV
KSSV 2021 年 8 月 2 日
編集済み: KSSV 2021 年 8 月 2 日
Read about unique.
A = [1 2 3 4 5 6; ...
2 3 4 2 3 1; ...
2 4 2 2 2 2; ...
1 2 3 1 1 1];
[c,ia,ib] = unique(A(:,1:3),'rows') ;
m = size(c,1) ;
n = size(A,2) ;
iwant = zeros(m,n) ;
iwant(:,1:3) = c ;
for i = 1:m
iwant(i,4:end) = sum(A(ib==i,4:end),1) ;
end
iwant
iwant = 3×6
1 2 3 5 6 7 2 3 4 2 3 1 2 4 2 2 2 2
  3 件のコメント
KSSV
KSSV 2021 年 8 月 2 日
Edited the answer.
Tom Engels
Tom Engels 2021 年 8 月 2 日
Perfect, that's exactly what I was looking for. Many thanks!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by