how i do weekly average?

조회 수: 1 (최근 30일)
Lilya
Lilya 2017년 9월 27일
편집: Tanziha Mahjabin 2020년 3월 6일
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