hourly average of multiple column data

I have made a code to calculate hourly average of minute data. But it works only for single column. I have multiple column of concentration (BC) data. I want hourly average of multiple columns How can i get the desired answers. pls help.
day1bc=BC(find((date==1)));
day1hour=hour(date==1);
maxday1=max(day1hour);
for i=1:maxday1
HI= find(day1hour==i);
Bcv=day1bc(HI);
mean_BC_hourly1(i,:)=mean(Bcv);
end
The input datas are:
day hour BC BC;
1 1 10 20;
1 1 20 40;
1 2 30 60;
1 2 40 80;
1 3 50 100;
1 3 60 120;
1 3 70 140;
The answer expected:
BC BC;
15 30;
35 70;
60 120;

 Accepted Answer

Use accumarray for the hours, although you would have to loop over the days:
% day hour BC BC
Mtx = [ 1 1 10 20;
1 1 20 40;
1 2 30 60;
1 2 40 80;
1 3 50 100;
1 3 60 120;
1 3 70 140;];
% ‘for’ % LOOP OVER DAYS
out(:,1) = accumarray(Mtx(:,2), Mtx(:,3), [], @mean);
out(:,2) = accumarray(Mtx(:,2), Mtx(:,4), [], @mean);
% ‘end’ % LOOP OVER DAYS
In the loop over days, replace the ‘:’ in the ‘out’ array reference with the day number.

4 Comments

Thanks star strider . But the day looping is not working. I have now two days data. But it is not fetching the proper result. kindly help. now the data set is
day hour BC BC; 1 1 10 60; 1 1 10 60; 1 2 20 80; 1 2 20 80; 1 3 30 100; 1 3 30 100; 1 3 30 100; 2 1 40 120; 2 1 40 120; 2 1 40 120; 2 2 50 140; 2 2 50 140; 2 2 50 140; 2 2 50 140; 2 2 50 140
I modified the code as below:But could not resolve. Please help
day=Mtx(:,1);
for day=1:2 % LOOP OVER DAYS
A(day,1) = accumarray(Mtx(:,2), Mtx(:,3), [], @mean);
A(day,2) = accumarray(Mtx(:,2), Mtx(:,4), [], @mean);
end
This works for the day loops. Note that the first three rows are for day #1 and the last two rows are for day #2. I added a ‘time’ array to keep track of them if you need to do that:
Mtx = [ 1 1 10 60;
1 1 10 60;
1 2 20 80;
1 2 20 80;
1 3 30 100;
1 3 30 100;
1 3 30 100;
2 1 40 120;
2 1 40 120;
2 1 40 120;
2 2 50 140;
2 2 50 140;
2 2 50 140;
2 2 50 140;
2 2 50 140];
dv = unique(Mtx(:,1)); % Unique Days
for k1 = dv(:)'
avg{k1,1} = accumarray(Mtx(Mtx(:,1)==k1,2), Mtx(Mtx(:,1)==k1,3), [], @mean); % BC1 Mean
avg{k1,2} = accumarray(Mtx(Mtx(:,1)==k1,2), Mtx(Mtx(:,1)==k1,4), [], @mean); % BC2 Mean
hr = accumarray(Mtx(Mtx(:,1)==k1,2), Mtx(Mtx(:,1)==k1,2), [], @mean); % Hours
time{k1} = [k1*ones(size(hr)) hr]; % [Days Hours]
end
Result = [cell2mat(time') cell2mat(avg)]
Result =
1 1 10 60
1 2 20 80
1 3 30 100
2 1 40 120
2 2 50 140
The ‘Result’ array concatenates the ‘time’ and ‘avg’ arrays (that I renamed to make them more meaningful).
Dear Star Strider Thanks again. It helped me so much. Thank you for your precious time ,effort and great help
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 May 2016

Commented:

on 13 May 2016

Community Treasure Hunt

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

Start Hunting!