Hi all,
I want to loop over the third dimension of the 3D matrix and calculate the average then sore the resulted data in a new matrix.
For example:
U10_series = [];
for day=1:length(datenum_array); % this time is corresponding to the number of days
filename=strcat('./MERRA2_400.tavg1_2d_ocn_Nx.2019',num2str(datevec_array(day,2),'%02i'),num2str(datevec_array(day,3),'%02i'),'.SUB.nc');
%read U10
U10 = ncread(filename,'U10M');
U10_box = U10(lon_range,lat_range,:);
U10_box_mean = nanmean(U10_box,3);
U10_series = [U10_series,U10_box_mean];
end
where am I mistaken?
The help is much appreciated

 채택된 답변

KSSV
KSSV 2019년 7월 17일

0 개 추천

U10 = ncread(filename,'U10M');
If your variable U10M is 3D, the above line reads complete data. Your U10 should be 3D. Now you can apply mean on it by specifying the dimension you want.
Also read about ncread, here you can specify the 3D dimension you want to read in a loop. If it is not clear, attach your file for further help.

댓글 수: 3

Lilya
Lilya 2019년 7월 17일
Thank you Sir KSSV!
It is understandable. but when loop through the 3rd dimension is given, the results are becoming repetitive; therefore, the new matrix store these repetitive data, which is not correct.
I've attached some of the files to check that also the first lines that can read the files. Thanks for the help.
datenum_array = [737426:1:737545]; %datenum(2019,01,01) to datenum(2019,04,31)
datevec_array = datevec(datenum_array);
filename=strcat('./MERRA2_400.tavg1_2d_ocn_Nx.20190101.SUB.nc');
lon=ncread(filename,'lon');
lat=ncread(filename,'lat');
lat_range=find(lat<= 27.6 & lat >= 26.8);
lon_range=find(lon<= 35.7 & lon >= 34.6);
ncfiles = dir('*.nc') ;
N = length(ncfiles) ;
for i = 1:N
ncfile = ncfiles(i).name ;
lon = ncread(ncfile,'lon') ;
lat = ncread(ncfile,'lat') ;
u = ncread(ncfile,'U10M') ;
v = ncread(ncfile,'V10M') ;
% do what you want
end
Lilya
Lilya 2019년 7월 17일
Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Agriculture에 대해 자세히 알아보기

질문:

2019년 7월 17일

댓글:

2019년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by