MATLAB: Combine (by adding) rows with the same entry in one of the columns

aggregateconditional sumsum

I think that might be a terrible title for a fairly straightforward problem:
I have sales data by zip code:
44202 35
44212 50
44202 10
I would like to sum over each zip code, so that the result is:
44202 45
44212 50
(I don't care about how the rows are ordered in the final product).
Thanks!

Best Answer

>> M = [44202,35;44212,50;44202,10]
M =
44202 35
44212 50
44202 10
>> [vec,~,idx] = unique(M(:,1));
>> [vec,accumarray(idx,M(:,2))]
ans =
44202 45
44212 50
Related Question