how i do weekly average?

10 次查看(过去 30 天)
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 个评论
Jan
Jan 2017-9-27
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.
Lilya
Lilya 2017-9-28
thanks Jan for your response. The attached MAT file is the date time of the corresponding measurements (978*744) which also has the same dimension of the hours per month (1*744). Therefore, I want to calculate the weekly average of the measurement from the date time reference matrix (1*744) going throw the index of the two matrices.
I hope this helps to clarify my idea.
thank you

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-9-28
编辑:Andrei Bobrov 2017-9-28
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 个评论
Lilya
Lilya 2017-9-29
:') Thanks so much.
Tanziha Mahjabin
Tanziha Mahjabin 2020-3-3
编辑:Tanziha Mahjabin 2020-3-6
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by