Average values after every n coloumns
Show older comments
Hi,
Sorry I am quite new to Matlab and I hope to be clear in the explanation.
I have a Matrix C = 174688 X 29225
Each row represent an observation taken from a certain location.
Each coloumn contains 3-hourly temperature data. Thus I have around 10 years (1 year = 2920 tri-hourly values) of tri-hourly temperature observations from 174688 locations.
I would like to create a new matrix containing, for each location, 2920 tri-hourly temperature values representing the average for the whole 10-years time period.
Thus, for instance, I need to average the 1st, the 2921th, the 5841th...., the 26281th temperature data for averaging the first tri-hourly time-step.
Thanks for your help.
Best
Accepted Answer
More Answers (1)
Adam Danz
on 22 Jun 2020
You probably already have a vector of timestamps that define each column of your data. The first line in the demo below produces a vector of datetime values for each column of your data. If you do not have such a vector, you should make one following the example I provided. The second line produces some fake data (much smaller than yours).
The demo uses datevec to extract the year/month/day/hour/minute/second values from the vector of timestamps. It then uses unique to group the month/day/hour/minute values into 29225 groups.
grpstats is used to compute the mean of the data for each group. Each column of the output avg is defined by the variable groupDefs.
% Create fake data
timestamps = datetime(1999,01,01) + minutes(cumsum(3.*ones(1,29225)));
data = rand(100, 29225);
% Get year/month/day/hour/min/sec data
dateMat = datevec(timestamps(:));
%group by same month/day/hour/minute
[groupDefs, groupID] = unique(dateMat(:,2:5),'stable', 'rows');
% group means (requires stats & machine learning toolbox)
avg = grpstats(data.',groupID,'mean')
Categories
Find more on Data Type Conversion 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!