MATLAB: How to aggregate rows in a matrix

aggregatematrix

If i have a matrix like the following example (or please see attached file)
    2008,5,4,9,40,0,0.0,0.0,0.0,15.9    2008,5,4,9,41,0,0.0,0.0,1.0,16.3    2008,8,5,16,20,0,0.0,0.0,1.0,2.9    2008,8,5,16,21,0,0.0,0.0,1.0,2.9
and so on for a full year. The matrix represents year, month, date of month, hour, minute, zone1,zone2,zone3 and zone4 How can i create a program that takes an hour for example and aggregates zone1,zone2zone3 and zone4 for that full choose hour ? I have to end up with two vectors. One with year,month,date of month, hour, minute and one with the aggregated zone1,zone2,zone3,zone4 The original matrix consists of data for a Whole year. I was think of something with Unique but then i am not sure how i can aggregate the data!
I have use for example [day, ~, subs] = unique(m(:, 1:3), 'rows') Then i get all the days. Then i can use sumdayzone1 = accumarray(subs, m(:, 7), [], @sum) but that only gives me the sum of zone1 on the choosen day. How can i do all zone together so i end up with only one vector to the corrensponding day ? I am a total newbie in this so any kind of help would be nice, thanks

Best Answer

accumarray only operates on vectors, so you have no choice but to call it once per column:
   [day, ~, subs] = unique(m(:, 1:3), 'rows');   colzones = [7 8 9 10];   sumdayzones = cell2mat(arrayfun(@(col) accumarray(subs, m(:, col), [], @sum), colzones, 'UniformOutput', false));
or with a loop
   [day, ~, subs] = unique(m(:, 1:3), 'rows');   colzones = [7 8 9 10];   sumdayzones = zeros(size(day, 1), numel(colzones));   for colidx = 1:numel(colzones)      sumdayzones(:, colidx) = accumarray(subs, m(:, colzones(colidx)), [], @sum);   end