Redefining a variable in a multidimensional array

2 ビュー (過去 30 日間)
Tim Holgate
Tim Holgate 2021 年 1 月 23 日
コメント済み: Tim Holgate 2021 年 2 月 5 日
Hi
I have a multidimenional array of lengths 887x1388x584. The 887 vector is the time vector in this case, and is in the wrong format, and the 1388 and 584 are lat and lon data (for mapping salinity). I have made a seperate vector (1x887) that has been corrected to the rigth time (from days since 1/1/1950 into calender date). My question is how do I translate this into the original array (replacing the time vector currently stored)?
My current code for making the arrray from .nc files is this:
%% Concat Loop
time_all=length(Nfiles);
sss_all=zeros(Nfiles, length(lon1), length(lat1));
for i = 1:Nfiles
ncfiles(i).name %this just helps my OCD in listing the name of the file
time = ncread(ncfiles(i).name, 'time');
sss = ncread(ncfiles(i).name, 'SSS');
sss_all(i, :, :) = sss;
time_all(i)=time;
end
%% SORT OUT TIME into standard date time
cortime=datetime(1950,1,1)+time_all;
I am very confused from reading the support articles how to do such a task. Thanks fr the help in advance!!

採用された回答

dpb
dpb 2021 年 1 月 23 日
編集済み: dpb 2021 年 1 月 23 日
There isn't a time vector explicitly stored in the 3D array; each slice(*) in the array is associated with the corresponding time in ordinal order, but the array contains only the coordinate data in each plane.
You couldn't actually include the datetime time vector explicitly into the 3D array anyway because of the different data classes (datetime versus double).
It's probably just as simple to leave as is and just use the vector of times as a lookup into the slices of the 3D array wanted.
(*) If my supposition is correct that sss is the 2D salinity array, I'd probably organize the 3D array as planes where time is the 3rd dimension by putting the variable subscript in the last position as
sss_all(:,:,i) = sss;
instead. To me at least, it's easier to visualize although the other way is just a slice along a colum index.
Alternatively, you could recast the data into a timetable where each row is a 2D cell array of coordinates.
After the fact with a set of random data had at hand in my workspace from a previous Answer demo, to create a timetable is something like
salinity=rand(10,5,10); % a dummy 3D array by plane for example
% combine salinity data as 2D array for each time step in timetable
ttSalinity=timetable(t20.Time(1:10),squeeze(mat2cell(salinity,10,5,ones(10,1))), ...
'VariableNames',{'Salinity'});
t20 is another table that happened to have a Time variable in it to save creating a new time vector.
The
squeeze(mat2cell(salinity,10,5,ones(10,1)));
converts the 3D array into a 2D cell array of the same number of rows as the height of the table; IOW each time step contains the 2D salinity data for that time as a cell array. It can't be a regular 2D array because the variables in a timetable must have the same number of rows as the number of times.
For the data at hand here, the above leads to:
ttSalinity =
10×1 timetable
Time Salinity
____________________ _____________
27-Oct-2020 13:35:46 {10×5 double}
27-Oct-2020 13:36:46 {10×5 double}
27-Oct-2020 13:37:46 {10×5 double}
27-Oct-2020 13:38:46 {10×5 double}
27-Oct-2020 13:39:46 {10×5 double}
27-Oct-2020 13:40:46 {10×5 double}
27-Oct-2020 13:41:46 {10×5 double}
27-Oct-2020 13:42:46 {10×5 double}
27-Oct-2020 13:43:46 {10×5 double}
27-Oct-2020 13:44:46 {10×5 double}
>>
Let's see, for your variables above this would look something like --
salinity=cell(Nfiles,1);
times=zeros(Nfiles,1);
for i = 1:Nfiles
disp(['Processing: ' ncfiles(i).name])
times(i)=ncread(ncfiles(i).name, 'time');
salinity{i}=ncread(ncfiles(i).name, 'SSS');
end
cortime=datetime(1950,1,1)+times;
ttSalinity=timetable(times,salinity,'VariableNames',{'Salinity'});
where I avoided the complexity of the squeeze operation by saving as a cell array on reading.
  1 件のコメント
Tim Holgate
Tim Holgate 2021 年 2 月 5 日
Thank you so much!!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by