0

I have a matrix with 24*7 rows(hourly data from the column week) and 52 columns(weeks in a year).

I want to calculate the average day (with 24h) from each week (resulting with a 24-by-52 matrix). Normaly I would reshape the vector from each week so that I could have 24 rows and 7 columns and then I could make the average from each hour, resulting in a average day from that week.

However, the year has 52 weeks, so this reshape is not realistic. Does anyone knows how to calculate the mean of each hour of a day in order to create one mean day using without reshaping the initial column for each week?

5
  • Do I get this correctly, you have a matrix, where each column represents the hour-values in one week (24*7) and now you want that average (resulting in a 1x52 vector?)? Or just the average for each day resulting in a 7x52 matrix?
    – rst
    Jun 4, 2015 at 13:35
  • or do you want just the overall day average 7x1 array?
    – Shai
    Jun 4, 2015 at 13:36
  • No. I have a matrix with 24*7 rows and 52 columns. Each column represents a week. I want a final matrix with 24 rows and 52 columns, so that i can represent each week for its average day.
    – PingP
    Jun 4, 2015 at 13:39
  • Are the values ordered like this: day1hour1;day1hour2;day1hour3 etc. or like this day1hour1;day2hour1;day3hour1 etc.?
    – rst
    Jun 4, 2015 at 13:42
  • day1hour1;day1hour2;day1hour3
    – PingP
    Jun 4, 2015 at 13:47

2 Answers 2

2

I don't understand why not use reshape?

reshaped = reshape( allData, [24 7 52] );
avg = mean( reshaped, 2 ); %// average over the seven days

You might want to squeeze the result at the end...

4
  • 1
    Was writing the same answer, I think this is the way to go (up you go)
    – rst
    Jun 4, 2015 at 13:46
  • With the reshape I wouldn´t be sure about the values because I'm not very good at matlab and once that reshape would be in 3D I wouldn´t to be able to confirm what I was doing. Anyway, thank you very much for your help. I've done this with a for loop, finally I got it. Thank you anyway!
    – PingP
    Jun 4, 2015 at 14:05
  • 1
    @PingP if you want to avoid reshaping into 3D you can reshape to 7x(24*52), then mean the columns, then reshape back.
    – buzjwa
    Jun 4, 2015 at 14:09
  • @PingP you can try the reshape thing as an exercise, after you implemented the for loop and then you can compare the two results to verify that you indeed got it right. this exercise should improve your understanding and confidence using Matlab.
    – Shai
    Jun 4, 2015 at 14:10
0

If you really really really don't want to use reshape, you can use accumarray.
First construct the indices relating the inputs in the 24*7-by-52 input to the 24-by-52 output:

rows = repmat( 1:24, [1 7*52] );
cols = repmat( 1:52, [7*24 1] );
avg = accumarray( [rows(:) cols(:)], allData(:), [24 52], @mean );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.