简体   繁体   English

创建3D数组(Matlab)

[英]Creating 3D array (Matlab)

I'm currently modelling ice sheet dynamics. 我目前正在建模冰盖动力学。 I have a loop that calculates the surface energy balance for 8760 hours (1:tmax) and for 8 different locations (1:no_stations). 我有一个循环计算表面能量平衡8760小时(1:tmax)和8个不同位置(1:no_stations)。 That looks like the following: 看起来如下:

tau=0.5;
albedo=0.35;
c0=-90;
c1=10;

for i=1:tmax
    for e=1:no_stations
        psi(i,e) = tau*(1-albedo)*insol(i,4)+c0+c1*temp_stations(i,e);
    end
end

The temperature data (temp_stations) is a 8760x8 array with the corresponding temperatures for the 8760 hours at the 8 locations, and insol(i,4) is an array of 8760x4 where the fourth colomn gives the evolution of the insolation with time. 温度数据(temp_stations)是一个8760x8阵列,相应的温度为8个位置的8760小时,而insol(i,4)是8760x4的阵列,其中第四个colomn随时间推移日照的演变。 My question: I want to create an extra dimension, a 8760x8x61 array where c0 is not constant but varies between -140 and -80: 我的问题:我想创建一个额外的维度,8760x8x61数组,其中c0不是常数,但在-140和-80之间变化:

c0=-140:1:-80;

How do I do this? 我该怎么做呢? I tried some things but it doesn't seem to work out fine. 我尝试了一些东西,但似乎没有成功。

Thanks! 谢谢!

For such cases that demand expansion, one can bring in bsxfun and permute for a vectorized solution, like so - 对于需要扩展的情况,可以为矢量化解决方案带来bsxfunpermute ,就像这样 -

parte2 = bsxfun(@plus,permute(c0,[1,3,2]),c1*temp_stations);
psi_out = bsxfun(@plus,tau*(1-albedo)*insol(:,4),parte2);

If you don't dig bsxfun or if you just want to verify the results from vectorized approach, here's the equivalent loopy code - 如果你不挖掘 bsxfun或者你只想验证矢量化方法的结果,这里是等效的循环代码 -

for i=1:tmax
    for e=1:no_stations
        for k = 1:numel(c0)
            psi(i,e,k) = tau*(1-albedo)*insol(i,4)+c0(k)+c1*temp_stations(i,e);
        end
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM