MATLAB: How to get a sum of column two if the value in column one is the same

if statementMATLABsum

How do I get a sum of column two if the value in column one is the same? The values below represent a segment of a much larger dataset
31128 166 31128 27 31128 0 31128 276 31128 0 31201 0 31201 2125 31201 0 31201 56 31201 0 31201 0 31201 0 31201 0 31235 2125 31235 13 31235 364 31235 1836 31235 379 31235 486

Best Answer

Use the unique and accumarray functions:
A = [31128 166
31128 27
31128 0
31128 276
31128 0
31201 0
31201 2125
31201 0
31201 56
31201 0
31201 0
31201 0
31201 0
31235 2125
31235 13
31235 364
31235 1836
31235 379
31235 486];
[A1u,ia,ic] = unique(A(:,1));
A2sum = accumarray(ic, A(:,2));
Result = [A1u A2sum]
Result =
31128 469
31201 2181
31235 5203