Can anyone help me with the a tentative guide on how to average across the two dimensions of a 3d array, and loop it across 86 timesteps?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rohit shaw
am 10 Okt. 2021
Kommentiert: Rohit shaw
am 17 Okt. 2021
Its a climate data of type 720x360x86. Thank you.
0 Kommentare
Akzeptierte Antwort
Chad Greene
am 11 Okt. 2021
Bearbeitet: Chad Greene
am 11 Okt. 2021
If you have a temperature data cube T whose dimensions correspond to lat x lon x time, the simplest way to get an 86x1 array of mean temperature time series is
Tm = squeeze(mean(mean(T,'omitnan'),'omitnan'));
then you can make a line plot of mean temperature as a function of time, like this:
plot(t,Tm)
However, I should point out that all of the grid cells in a geographic grid have areas that depend on latitude, so it's not appropriate to give equal weight to a polar and equatorial grid cells (they're very different sizes!). In the Climate Data Toolbox for Matlab, see Example 3 of wmean for what I'm talking about.
It would be much better to get the area of each grid cell in your global grid using cdtarea like this:
A = cdtarea(Lat,Lon);
Then calculate the weighted mean like this:
Tm = NaN(86,1); % (preallocate for efficiency)
% Loop through each time step:
for k = 1:86
Tm(k) = wmean(T(:,:,k),A,'all');
end
% Get a mask of the grid cells that always have valid data:
mask = all(isfinite(T),3);
% Calculate weighted mean temperature time series in the finite grid cells:
Tm = local(T,mask,'weight',A);
Weitere Antworten (1)
Dave B
am 10 Okt. 2021
Let's do this with a smaller array, as the specific numbers don't seem particularly important. I'll do 5x4x3 so we can see the values easily. It's actually very easy because MATLAB works naturally with matrices of any shape, you can do this all with the mean function, no loops required:
X = rand(5,4,3)
a=mean(X,1) % mean across rows (same as mean(X))
b=mean(X,2) % mean across columns
You might find the shape of these outputs to be annoying, the squeeze function is good for fixing that up:
squeeze(a)
squeeze(b)
12 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!