how i do weekly average?
10 views (last 30 days)
Show older comments
Hi all,
I have a matrix of 978*744 which is = no. observation*hours in a month. and the attached mat file is the DateTime vector. I want to make a weekly average.
Thank you in advance.
2 Comments
Jan
on 27 Sep 2017
The MAT file contains the datetime vector of what? I do not have access to a Matlab engine currently, therefore I cannot inspect the MAT file. Otherwise perhaps I could guess, what it contains. But maybe guessing is less efficient than if you explain it.
Accepted Answer
Andrei Bobrov
on 28 Sep 2017
Edited: Andrei Bobrov
on 28 Sep 2017
Let data - your array [978 x 744].
For MATLAB >= R2016b (here week -> [Sun : Sat]):
Time = (datetime([2017 7 1 0 0 0]):hours(1):datetime([2017 7 31 23 0 0]))';
% or: Time = datetime(timeCopy);
TT = array2timetable(data','R',Time);
TT_out = retime(TT,'weekly','mean');
For MATLAB < R2016b ( here week -> [Mon : Sun]):
data = data';
Time = (datetime([2017 7 1 0 0 0]):hours(1):datetime([2017 7 31 23 0 0]))';
wd = rem(weekday(Time) + 5, 7) + 1;
ii = [true;diff(wd == 1) == 1];
g = cumsum(ii);
[r,c] = ndgrid(g,1:size(TT,2));
TT_out = [table(Time(ii),'V',{'Time'}),...
array2table(accumarray([r(:),c(:)],data(:),[],@mean))];
2 Comments
Tanziha Mahjabin
on 3 Mar 2020
Edited: Tanziha Mahjabin
on 6 Mar 2020
Hi,
what about the array with 3 dimensions? I have daily average of SST data (340X285X36) which are (lon,lat,time). I want to have weekly average.
Is the [r,c] line correct? Then how can i mean it? And if possible how can i modify the 'for loop'?So far i have this:
clear all; close all; clc;
path(path,'D:\Radar_job\SST')
ncfile='IMOS_aggregation_20200212T151211Z.nc'; %short time
time2=ncread(ncfile,'time');
time2=ncread(ncfile,'time')./86400+datenum(1981,01,01);
SST_lon=ncread(ncfile,'lon');
SST_lat=ncread(ncfile,'lat');
SST=ncread(ncfile,'sea_surface_temperature');
SST=SST-273.15;
id1=find(SST_lat>-39.9&SST_lat<-36.2);
id2=find(SST_lon>137.1&SST_lon<141.9);
lat1=SST_lat(id1);
lon1=SST_lon(id2);
tvec=datevec(time2);
t=datetime(tvec)
[C,uq,iuq]=unique((tvec(:,1:3)),'rows','stable'); %manually look which rows are full weeks
time3=datenum(C);
wd = rem(weekday(time3) + 5, 7) + 1;
ii = [true;diff(wd == 1) == 1];
g = cumsum(ii);
[r,c] = ndgrid(g,1:size(SST,3));
for i=1:length(time3)
h=figure;
set(gcf,'Position',[200 100 1500 800])
set(gcf,'PaperPositionMode','auto')
m_proj('mercator','lon<gitude>',[137.1 141.9],'lat<itude>',[-39.9 -36.2]);
m_pcolor(lon1,lat1,(SST(id2,id1,i))');
shading interp;
m_proj('mercator','lon<gitude>',[137.1 141.9],'lat<itude>',[-39.9 -36.2]);
m_gshhs_h('patch',[0.7 0.7 0.7],'LineWidth',1.5);
m_grid('ytick',[-90:0.5:90],'xtick',[-180:0.5:180],'tickdir','in','FontSize',14);
colormap jet
colorbar
caxis manual
caxis([15 19])
end
More Answers (0)
See Also
Categories
Find more on Dates and Time 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!