for loops, 1 hour averages, 8 hour averages

2 views (last 30 days)
Amanda Goddard
Amanda Goddard on 13 Feb 2015
Edited: Star Strider on 13 Feb 2015
I have an excel sheet of 40,000 values of ozone concentrations per minute. I am to calculate all the 1-hour averages and then 8-hour averages of all the data.
Instead of doing this by hand in excel, I thought I could use a for loop in matlab.
I need the for loop to calculate the average for the first 60 columns of data, and then the successive 60 columns of data, and so on and so forth.
However I'm unsure of how to create a function for the for loop to do so.
Any suggestions/examples?

Answers (1)

Star Strider
Star Strider on 13 Feb 2015
Edited: Star Strider on 13 Feb 2015
If each hour is a single column, and you have your data in a (60xN) matrix in MATLAB, then the mean function will take the column means automatically.
For example:
N = 24; % Number Of Columns (Hours)
Ozone = randi(50, 60, N); % Created Data
OzoneHourMeans = mean(Ozone); % Column (Hour) Means
Ozone8HourMeans = mean(reshape(OzoneHourMeans, 8, []));
If your data are in a single column and you have data for every minute, use the reshape function to get your single column into a (60xN) matrix. Note that if your single column has a row size that is not an integer multiple of 60, you will have to trim the ‘excess’ minutes before you use reshape.
To get your 8 hour averages, do essentially the same manouever on your vector of hourly averages with reshape to get an (8xM) matrix. Then take the means of those columns.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!