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?

9 次查看(过去 30 天)
Its a climate data of type 720x360x86. Thank you.

采纳的回答

Chad Greene
Chad Greene 2021-10-11
编辑:Chad Greene 2021-10-11
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
Alternatively, you could skip the loop and use the local function like this:
% 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);

更多回答(1 个)

Dave B
Dave B 2021-10-10
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)
X =
X(:,:,1) = 0.8932 0.0985 0.1182 0.5943 0.9755 0.9365 0.2791 0.4799 0.2896 0.9367 0.1734 0.3763 0.4609 0.2365 0.5636 0.9141 0.5024 0.1773 0.6540 0.3962 X(:,:,2) = 0.7910 0.0458 0.9640 0.3480 0.2623 0.8134 0.1968 0.5781 0.9564 0.0540 0.6005 0.1332 0.0680 0.9841 0.9569 0.4038 0.8187 0.9467 0.5550 0.4149 X(:,:,3) = 0.0716 0.7523 0.6300 0.1288 0.4460 0.9575 0.0197 0.8113 0.7260 0.7879 0.5096 0.4921 0.9718 0.0751 0.3600 0.1038 0.0820 0.9162 0.4284 0.8737
a=mean(X,1) % mean across rows (same as mean(X))
a =
a(:,:,1) = 0.6243 0.4771 0.3577 0.5522 a(:,:,2) = 0.5793 0.5688 0.6546 0.3756 a(:,:,3) = 0.4595 0.6978 0.3895 0.4819
b=mean(X,2) % mean across columns
b =
b(:,:,1) = 0.4261 0.6678 0.4440 0.5438 0.4325 b(:,:,2) = 0.5372 0.4627 0.4360 0.6032 0.6838 b(:,:,3) = 0.3957 0.5586 0.6289 0.3777 0.5751
You might find the shape of these outputs to be annoying, the squeeze function is good for fixing that up:
squeeze(a)
ans = 4×3
0.6243 0.5793 0.4595 0.4771 0.5688 0.6978 0.3577 0.6546 0.3895 0.5522 0.3756 0.4819
squeeze(b)
ans = 5×3
0.4261 0.5372 0.3957 0.6678 0.4627 0.5586 0.4440 0.4360 0.6289 0.5438 0.6032 0.3777 0.4325 0.6838 0.5751
  12 个评论
Rohit shaw
Rohit shaw 2021-10-11
Yes @Dave B, thank you for the reply. I have saved the output rendered by reshape and would definitely drop the comment here about how it goes. It's just that I am quite new to such things hence the constant barge of queries. Anyways, thanks for ur constant replies. Have a goood evening ahead.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by