MATLAB: How to use accumarray

accumarraygrouping data

Solved!

Best Answer

The hard bit for accumarray is finding out how to create the subs vector, which is basically a vector telling how to partition the rows of the matrix according to your criteria (same day, or same month, etc.).
The best tool for this in your case is probably unique. Pass the relevant columns of your matrix to unique with the 'rows' option and the third return argument should be the subs of accumarray.
For example, for a daily consumption, the criteria is same year, same day and same month, so:
m = [2008 1 1 12 0 0 1 2 3 4;
2008 1 1 13 0 0 11 12 13 14;
2008 1 1 14 0 0 2 2 1 1;
2008 2 2 12 0 0 1 2 3 4;
2008 2 2 13 0 0 11 12 13 14;
2009 5 6 11 1 1 1 2 3 4]; %for example
[day, ~, subs] = unique(m(:, 1:3), 'rows') %1:3 as the criteria is based on these columns
meandailyzone1 = accumarray(subs, m(:, 7), [], @mean)
You do the same for the other criteria, varying the columns you pass to unique