Sum matrix with repeating indices

2 views (last 30 days)
Emma Kuttler
Emma Kuttler on 20 Mar 2022
Edited: Stephen23 on 21 Mar 2022
Hi! I have one matrix, A, that has one column of identifier values (with repeats and some missing numbers) and a column of values. I'd like to produce a column of all of the unique values and then another column of their sum. For example, if A is as follows, i want to produce B.
A = [1 2
1 -3
2 0
3 3
5 6
2 -4
4 0.5
7 9
7 2]
% I want to produce B
B = [1 -1
2 -4
3 3
4 0.5
5 6
7 11]

Accepted Answer

Stephen23
Stephen23 on 20 Mar 2022
Edited: Stephen23 on 21 Mar 2022
A simple MATLAB approach:
A = [1,2;1,-3;2,0;3,3;5,6;2,-4;4,0.5;7,9;7,2]
A = 9×2
1.0000 2.0000 1.0000 -3.0000 2.0000 0 3.0000 3.0000 5.0000 6.0000 2.0000 -4.0000 4.0000 0.5000 7.0000 9.0000 7.0000 2.0000
[G,U] = findgroups(A(:,1));
M = [U,accumarray(G,A(:,2))]
M = 6×2
1.0000 -1.0000 2.0000 -4.0000 3.0000 3.0000 4.0000 0.5000 5.0000 6.0000 7.0000 11.0000
  2 Comments
Steven Lord
Steven Lord on 21 Mar 2022
Instead of using findgroups and accumarray I would probably use groupsummary.
A = [1,2;1,-3;2,0;3,3;5,6;2,-4;4,0.5;7,9;7,2];
[values, groups] = groupsummary(A(:, 2), A(:, 1), @sum);
results = [groups, values]
results = 6×2
1.0000 -1.0000 2.0000 -4.0000 3.0000 3.0000 4.0000 0.5000 5.0000 6.0000 7.0000 11.0000

Sign in to comment.

More Answers (1)

Voss
Voss on 20 Mar 2022
Edited: Voss on 20 Mar 2022
One perfectly valid MATLAB approach:
A = [1 2
1 -3
2 0
3 3
5 6
2 -4
4 0.5
7 9
7 2];
[uA,~,jj] = unique(A(:,1));
n_uA = numel(uA);
B = [uA zeros(n_uA,1)];
for ii = 1:n_uA
B(ii,2) = sum(A(jj == ii,2));
end
disp(B)
1.0000 -1.0000 2.0000 -4.0000 3.0000 3.0000 4.0000 0.5000 5.0000 6.0000 7.0000 11.0000
% % I want to produce B
% B = [1 -1
% 2 -4
% 3 3
% 4 0.5
% 5 6
% 7 11];

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by