how to store data in a matrix?

1 回表示 (過去 30 日間)
Lilya
Lilya 2019 年 7 月 17 日
コメント済み: Lilya 2019 年 7 月 17 日
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 日
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 件のコメント
KSSV
KSSV 2019 年 7 月 17 日
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 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by