create a new 4D Array from 2 others

1 回表示 (過去 30 日間)
Pepe Grillo
Pepe Grillo 2021 年 2 月 24 日
コメント済み: Pepe Grillo 2021 年 2 月 25 日
Hi!
i want to build a new 4d array from 2 others and one array is telling me the position of the values that i want,
any idea?
Thanks!
  7 件のコメント
Pepe Grillo
Pepe Grillo 2021 年 2 月 25 日
Ok. A level of depth means that there is 7 possible depths distance each other by 500 meters (but thats not the problem)
Lets see if I can explain better and thanks ofcourse for the interaction!
so the 4D array u1 = (241x97x7x1637) (lon, lat, dpth, time). for different lat lon you have different depths. What I want is to have a 3D array u2= (lon, lat, time) with u values that are the last values form u1.
Pepe Grillo
Pepe Grillo 2021 年 2 月 25 日
last value from u1 means that not allways the last value is the at the level 7 because the bathymetrie is changing and could be at 5 or 3 or 1
something like that
let me know
thanks!

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

採用された回答

Rik
Rik 2021 年 2 月 25 日
I think I understand what you mean. The code below will overwrite all values that are not NaN for a given depth, which is equivalent to setting it to the last non-NaN (except if the first value is NaN).
%generate some fake data
%cumsum will ensure all values after the first NaN are NaN as well
u1=rand(241,97,7,1637);u1(u1<0.1)=NaN;u1=cumsum(u1,3);
u2=u1(:,:,1,:);
for depth=1:size(u1,3)
layer=u1(:,:,depth,:);
L=~isnan(layer);
u2(L)=layer(L);
end
  3 件のコメント
Rik
Rik 2021 年 2 月 25 日
That first line was only meant to generate random data. You should replace it with your actual data.
Pepe Grillo
Pepe Grillo 2021 年 2 月 25 日
yes sure, just the second part works perfect
thk!

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by