0
votes

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?

2
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
or do you want just the overall day average 7x1 array?Shai
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
Are the values ordered like this: day1hour1;day1hour2;day1hour3 etc. or like this day1hour1;day2hour1;day3hour1 etc.?rst
day1hour1;day1hour2;day1hour3PingP

2 Answers

2
votes

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...

0
votes

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 );