MATLAB: Sum up rows of several columns with the same element in the first column

array sumsummarize rows

I have some problems with analysing the output of an image analysis.
I have a 58×41 cell array. For simplification, I shorten it down to a 10×5 cell array:
h =
'10.632' '9871.3017' '24091.8402' '6401.6857' '1123.9601'
'10.632' '3958.2942' '11044.1296' '3029.8055' '879.6209'
'10.632' '8893.9451' '23945.2368' '5766.404' '1563.7706'
'10.632' '13878.4638' '33132.3888' '10506.5835' '2296.788'
'10.632' '4691.3117' '12803.3715' '3371.8803' '1319.4314'
'10.632' '3371.8803' '6792.6284' '2296.788' '586.414'
'10.632' '21306.3739' '51066.8825' '14904.6882' '3274.1446'
'10.632' '15002.4238' '42319.5409' '14171.6707' '3420.7481'
'9.736' '7867.7206' '21013.1669' '4642.4439' '781.8853'
'9.736' '9382.6234' '21306.3739' '6597.1571' '1466.0349'
Column 1 represents the water depths at which the images were taken. The other columns show the number of particles per litre, whereas each column represents another size spectrum of the particles.
My aim is to sum up the number of particles per litre for each depth and each size spectrum, so that I get a cell array like this:
houtput =
'10.632' '122267.311' '205196.0187' '60449.5059' '14464.8777'
'9.736' '17250.344' '42319.5408' '11239.601' '2247.9202'
What I tried until now is this:
numcols = size(h,2)
depths = {h{2:end,1}};
[uniqueDepths, firstInd, allInd] = unique(depths);
totalNumberArray = zeros(2,numcols);
for j = 2:2:numcols
amount = [z{2:end,j}];
for i = 1:numel(firstInd)
totalNumber(i) = sum(amount(allInd == i));
end
totalNumberArray(j) = totalNumber(i)
end
out = [uniqueDepths;num2cell(totalNumber)];
However, this gives me not what I want to have. The particles are certainly not summed up and the depths are in a row and not in a column in the end. What else can I try?
Thanks a lot in advance!

Best Answer

h ={'10.632' '9871.3017' '24091.8402' '6401.6857' '1123.9601'; ...
'10.632' '3958.2942' '11044.1296' '3029.8055' '879.6209' ; ...
'10.632' '8893.9451' '23945.2368' '5766.404' '1563.7706'; ...
'10.632' '13878.4638' '33132.3888' '10506.5835' '2296.788' ; ...
'10.632' '4691.3117' '12803.3715' '3371.8803' '1319.4314'; ...
'10.632' '3371.8803' '6792.6284' '2296.788' '586.414' ; ...
'10.632' '21306.3739' '51066.8825' '14904.6882' '3274.1446'; ...
'10.632' '15002.4238' '42319.5409' '14171.6707' '3420.7481'; ...
'9.736' '7867.7206' '21013.1669' '4642.4439' '781.8853'; ...
'9.736' '9382.6234' '21306.3739' '6597.1571' '1466.0349'};
D = str2double(h);
[Group, ID] = findgroups(D(:, 1));
Result = splitapply(@sum, D(:, 2:end), Group);
houtputN = [ID, Result];
houtput = num2cell(houtputN);
Abd if you really want to have the output as a cell string, although working with strings seems to be strange here:
houtputC = sprintfc('%f', houtputN);
I strongly recommend to work with numerical values instead of strings.
And if you use an older Matlab version without splitapply:
D = str2double(h);
ID = unique(D(:,1));
nID = numel(ID);
Result = zeros(nID, size(D, 2) - 1);
for k = 1:nID
Result(k, :) = sum(D(D(:,1) == ID(k), 2:end), 1);
end
% Depending on what kind of output you want:
houtputN = [ID, Result];
houtput = num2cell(houtputN);
houtputC = sprintfc('%f', houtputN);