Arrange be in order of their pairing, and count how many times they appear

1 回表示 (過去 30 日間)
JL
JL 2019 年 8 月 12 日
コメント済み: JL 2019 年 8 月 17 日
I have 2 matices, A and B. What I want to do is for B to be rearranged like C, and remove any repreated row.
A = [1; 2; 3; 4; 5; 6;];
B = [1 2; 2 3; 1 2; 1 3; 1 4; 1 2;]
I would like to produce C with A and B
C = [1 2;
1 3;
1 4;
2 3;]
Thereafter, after reordering them, I want to add the same pair i.e. add all the 1 2s together like in D.
D = [1+3+6;
4;
5;
2;];
= [9;
4;
5;
2;];

採用された回答

the cyclist
the cyclist 2019 年 8 月 12 日
[C,~,idx] = unique(B,'row');
D = accumarray(idx,A);
1 + 3 + 6 = 10. ;-)
  2 件のコメント
dpb
dpb 2019 年 8 月 12 日
My interpretation is that D should be
>> accumarray(ix,sum(B,2))
ans =
9
4
5
5
>>
but the description is confusing at best and the answer presented isn't correct whichever is the actual definition wanted...
JL
JL 2019 年 8 月 17 日
thanks guys!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by