How to get sum of all the order values for a given id of a column in matrix

조회 수: 1 (최근 30일)
i have 2 columns and 10 rows
1 10
2 29
2 34
2 79
3 11
3 20
3 11
4 9
4 1
5 10
i want the add numbers having same value in first column The answer i want is using loop or any other operation
1 10
2 142
3 42
4 10
5 10

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 19일
편집: Azzi Abdelmalek 2014년 12월 19일
A=[1 10;2 29;2 34;2 79;3 11;3 20;3 11;4 9;4 1;5 10]
out=[unique(A(:,1)) accumarray(A(:,1),A(:,2))]
  댓글 수: 1
Mitul saraiya
Mitul saraiya 2014년 12월 20일
Actually i have to work on a matrix with 2 columns but more than 100 rows when i tried using accumarray it gives error as Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in mm (line 26) out=[unique(mannu(:,1)) accumarray(mannu(:,1),mannu(:,2))];
mannu is the matrix with 2 columns and 100 rows

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by