How to sum up unique arrays in the matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Moe
el 4 de Abr. de 2016
Editada: Azzi Abdelmalek
el 4 de Abr. de 2016
Hi everyone,
Suppose the matrix A is as follows:
A = [10 1 180
10 1 40
10 1 40
10 1 160
10 2 10
10 3 410
10 1 10
10 1 50
12 1 30
12 1 30
12 2 45
12 3 60
65 1 15
65 1 360
];
I want to sum up the values in column three if the value in column one and two are similar. The output matrix should be like:
B = [10 1 420
10 2 10
10 3 410
10 1 60
12 1 60
12 2 45
12 3 60
65 1 375
];
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160053/image.jpeg)
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 4 de Abr. de 2016
Editada: Azzi Abdelmalek
el 4 de Abr. de 2016
Edit
A = [10 1 180
10 1 40
10 1 40
10 1 160
10 2 10
10 3 410
10 1 10
10 1 50
12 1 30
12 1 30
12 2 45
12 3 60
65 1 15
65 1 360
];
idx=any([0 0;diff(A(:,1:2))],2)
jdy=cumsum(idx)
b=accumarray(jdy+1,1:numel(jdy),[],@(x) sum(A(x,3)))
[ii,jj,kk]=unique(jdy)
out=[A(jj,1:2) b]
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!