Stacking 2D matrix to 3D using time as a third variable

9 ビュー (過去 30 日間)
Ashal
Ashal 2022 年 12 月 9 日
コメント済み: Peter Perkins 2022 年 12 月 12 日
Hi I am a beginner in matlab and I am trying to stack 2D matrix into 3D matrix. I have variable sst as 1440*720 and time variable as 1*366. These are all daily data. How do I stack time to sst so that I have 366*1440*720 matrix. I have tried using cat and meshgrid but nothing seems to work. Thank you.
data.time = cat(3,data.time,data.sst)
or
[data.sst1]=meshgrid(data.sst,data.time); (this multiplies 1440*720 and gives me 366*1036800 matrix)
  3 件のコメント
Ashal
Ashal 2022 年 12 月 9 日
編集済み: Ashal 2022 年 12 月 9 日
Thank you. I do have individual 366 1440*720 and I am using the following code to read data. So basically I have 366 different files that has sst(1440*720) and time values Here ii =366. I wish I could add data here but it is a huge folder.
gunzip("2020.zip.gz")
!ls 2020/oisst-*.nc>data.ptr
x = importdata('data.ptr'); %here x is cell array
for ii = 1:length(x);
data.time(ii) = ncread(x{ii},'time');
%ncdisp("2020/oisst-avhrr-v02r01.20200101.nc"); %ncdisp look inside nc file
data.sst = ncread(x{ii}, 'sst');
end
Peter Perkins
Peter Perkins 2022 年 12 月 12 日
Just out of curiousity ...
366, OK, days in 2020 (although you may have a bug when you work on a non-leap year)
1440, OK, so maybe 24*60 minutes in a day?
What does 720 represent?
Or is 1440x720 a spatial lat/lon grid?

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 12 月 9 日
編集済み: Voss 2022 年 12 月 9 日
nx = numel(x);
data = struct( ...
'time',zeros(1,nx), ...
'sst',zeros(nx,1440,720);
for ii = 1:nx
data.time(ii) = ncread(x{ii},'time');
%ncdisp("2020/oisst-avhrr-v02r01.20200101.nc"); %ncdisp look inside nc file
data.sst(ii,:,:) = ncread(x{ii}, 'sst');
end
That makes data.sst a 366*1440*720 array, as requested.
  2 件のコメント
Ashal
Ashal 2022 年 12 月 9 日
It worked. Thank you so much.
Voss
Voss 2022 年 12 月 9 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by