MATLAB Answers

How to use accumarray?

55 views (last 30 days)
Salima
Salima on 18 Nov 2014
Edited: Salima on 21 Nov 2014
Solved!

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 18 Nov 2014
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

  0 Comments

Sign in to comment.

More Answers (2)

Kelly Kearney
Kelly Kearney on 18 Nov 2014
A small variation in Guillaume's suggestion, in case you want a value for every day/hour/hour of day (even if no data was collected then). In this case, you can specify the expected output size and fill value:
data = [datevec(now + sort(rand(1000,1)*60)) rand(1000,4)];
dn = datenum(data(:,1:6));
day = floor(dn);
hr = floor(dn * 24);
hrofday = floor((dn - floor(dn))*24);
didx = day - min(day) + 1;
daily = zeros(max(didx), 4);
for ii = 1:4
daily(:,ii) = accumarray(didx, data(:,6+ii), [max(didx) 1], @mean, NaN);
end

  0 Comments

Sign in to comment.


Kevin Claytor
Kevin Claytor on 18 Nov 2014
You could use a Table structure. First, I'd convert the first few elements to a datetime object , so that they're easier to work with. Using your example above;
mytime = [2008,1,5,15,8,0; 2008,1,6,15,8,0]; % Demo with different days
% mytime = data(:, 1:6); % Extract the date info from the data array
DateAndTime = datetime(mytime)
DateAndTime =
05-Jan-2008 15:08:00
06-Jan-2008 15:08:00
Now we can put everything into a table;
Zone1 = data(:, 7);
Zone2 = data(:, 8);
T = table(DateAndTime, Zone1, Zone2);
Now we can start to really have fun. Let's see what the consumption by hour looks like;
% First get the hour out of the datetime object and add it as a new column to our table
T.Hour = hour(T.DateAndTime);
% Now group the results by hour, summing all the values for a given hour;
EnergyByHour = varfun(@sum, T, 'InputVariables', 'Zone1', 'GroupingVariables', {'Hour'})
% And plot the results
plot(EnergyByHour.Hour, EnergyByHour.sum_Zone1)
Check out the docs for more example cases of hour you can use the Table format, and how to apply functions across the rows / columns of your tables to get exactly what you're after. It's a pretty neat data structure, although some of the function calls can be a bit opaque (like varfun above).

  0 Comments

Sign in to comment.

Sign in to answer this question.