MATLAB: Reduce matrix data by combining values

combinematrixmodify matrixreduce

Dear matlab community,
I need to reduce data in a matrix of two columns, and many rows (almost 2 billion, so I would like to find a way to automate it!)
The structure is as follows:
Column 1 has a bunch of discrete values, which occur repeatedly.
Example:
a
b
a
c
b
Column 2 has values in it, that should be added up for all "a", all "b", and all "c", that only one "a", "b", "c" is left in the final matrix.
Example:
Initial matrix:
a 10
b 5
a 1
c 20
b 3
The final matrix should look like this:
a 11
b 8
c 20
Thank you very much! A solution would help me a lot!

Best Answer

In the below code I assume your array is a cell array (because you cannot mix letters and numbers in a numeric array).
[unique_keys, ~, idx] = unique(YourArray(:,1));
vals_to_total = cell2mat( YourArray(:,2) );
totals = accumarray(idx, vals_to_total);
results = [unique_keys, num2cell(totals)];