How to use accumarray?
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Respuesta aceptada
Guillaume
el 18 de Nov. de 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 comentarios
Más respuestas (2)
Kelly Kearney
el 18 de Nov. de 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 comentarios
Kevin Claytor
el 18 de Nov. de 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 comentarios
Ver también
Categorías
Más información sobre Data Preprocessing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!