How can i sum every 12th block of data from a large data set?

I have a set of data for energy performance sampled every 5 minutes. I would like to condense the energy outputs into hourly totals - hence blocks of 12.
I have imported the data into a variable array from Excel, called 'unit1'. the data i want lies in column 9:
>> y=(unit1(1:124390,9));
As one can see it is a large data set.
All i know so far is that i can pick out every 12th value:
>> y=(unit1(1:12:124390,9));
but that's as far as i have got - some others have done resampling but it is not clear.
In Brief - lots of data, tell the sum of every block of 12 in a continous fashion.
I then hope to produce a frequency distribution of the energy output over a period of one year (or 8760 hours)

2 Comments

124390 is not a multiple of 12. What do you plan on doing with the incomplete hours? Do the first 12 data points belong to one hour, or do they span two hour bins?
I kept it simple for the question - i will use only the first complete years number of values (24x365 = 8760) so after calculating the block-sums of 12 in an new array, plot only the first 8760. the first 12 points do cover one hour. incomplete hours will be at the end and ignored.

Sign in to comment.

 Accepted Answer

You can go for a solution around:
n = size(unit1, 1) ; % Should be 124390,
id = zeros(n, 1) ; % Build vector of ids:
id(1:12:n) = 1 ; % 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 ...
id = cumsum(id) ;
sumHour = accumarray(id, unit1(:,9)) ;

2 Comments

Thats brilliant - thank you very much.
I need to sum variable by months so I get monthly sums:
Year=randn(1,365);
months365=[31 28 31 30 31 30 31 31 30 31 30 31];
any help is appreciated.
thanks

Sign in to comment.

More Answers (1)

Here it is for data that is a multiple of 12 long:
% Create sample data a multiple of 12 long.
unit1 = rand(124392, 9);
% Exact column #9.
column9 = unit1(:,9);
% Reshape it into a 10366 row by 12 column matrix.
reshapedColumn9 = reshape(column9, [length(column9)/12, 12]);
% Get the means going cross columns.
theMeans = mean(reshapedColumn9, 2);
% These are the means of blocks of 12 in the original matrix.
Of course someone will pack that all into a single line but I made it explicitly in several lines and put in comments so that you can follow the thought process more easily.

1 Comment

I'm voting for this solution; I proposed ACCUMARRAY before the OP wrote the comment indicating that he would truncate the data to a multiple of 12, and CUMSUM/ACCUMARRAY were a solution for managing situations where the "multiple of 12" condition is not necessary.

Sign in to comment.

Categories

Products

Asked:

on 17 Apr 2013

Commented:

on 21 Oct 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!