comparing matrix elements and performing operation if equal

If I have a matrix
A=
18 18 1
17 20 2
18 18 1
17 25 2
19 16 3
18 18 1
19 17 3
I want to scan through the matrix and when ever the first column elements are equal, the 3rd column elements should get added up
e.g: the resultant matrix should be
18 18 1
17 20 2
18 18 2
17 25 4
19 16 3
18 18 3
19 17 6
Please help me in this

1 件のコメント

N/A
N/A 2019 年 7 月 5 日
編集済み: N/A 2019 年 7 月 5 日
My problem is also the same i just have one little extension that whenever the elemnts in column 1 and 3 both are repeated elsewhere in matrix then only third column should get added up
e.g
A=
18 18 1
17 20 2
18 18 1
17 25 2
19 17 3
18 18 1
19 17 3
then answer should be
18 18 1
17 20 2
18 18 2
17 25 2
19 17 3
18 18 3
19 17 6

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

 採用された回答

Simon
Simon 2013 年 11 月 14 日
編集済み: Simon 2013 年 11 月 14 日

0 投票

Hi!
% get indices of unique values
[~, ~, c] = unique(A(:, 1));
% loop over all unique values
for n = 1:max(c)
ind = n==c;
% cumulative sum
cs = cumsum(A(ind, 3));
% set third column
A(ind, 3) = cs;
end

その他の回答 (0 件)

カテゴリ

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

タグ

質問済み:

2013 年 11 月 14 日

編集済み:

N/A
2019 年 7 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by