Aggregating numbers based on first column index

Hi
I have a matrix as follows
x=[1 3;
1 4;
1 6;
2 5;
2 7;
3 4;
3 11;
3 1;
3 2]
I want to write a code or a loop that aggregates the numbers in the second column for each different number in the first column.
The result should be
y= [1 13;
2 12;
3 18]

 採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 4 日

2 投票

[unique_keys, ~, uq_idx] = unique(x(:,1));
totaled_data = accumarray(uq_idx, x(:,2));
y = [unique_keys, totaled_data];

1 件のコメント

Mido
Mido 2016 年 11 月 5 日
I am really thankful to you. This solved my problem.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 11 月 4 日

コメント済み:

2016 年 11 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by