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

Rik
Rik 2021년 2월 24일
Here's my idea: post a better description of your input data.
Walter Roberson
Walter Roberson 2021년 2월 24일
Is the position information linear indexes? Is it column numbers with the row number implied by the row number of the value?
Pepe Grillo
Pepe Grillo 2021년 2월 25일
Hi, Yes sorry for so short description.
Here is the problem: I have initially a array 4D (time, depth, lat, lon) and the variable u (velocity). There is 7 levels of depth but what I want is to get the last values of velocity before the NaN, that means the seefloor. What I did in base of another post was to make an array 4D with the number of values before the NaN but not the value itself. So this new 4D gives me an idea of the bathymetrie that is cool but I want to know the velocitys close to the bottom.
this I make to get this array with the number of values non NaN before the first NaN in depth:
[~, idx] = max(cumsum(~isnan(u1), 3), [], 3)
I try with find also in the formula but just gives me nonsennse.
Thanks!
Jan
Jan 2021년 2월 25일
At "There is 7 levels of depth" I cannot follow you anymore. What is a level of depth?
Rik
Rik 2021년 2월 25일
What values are in your 4D array and what shape is your u variable?
And can you confirm you mean the lenght along the second dimension of your 4D array is 7? So you have an t-by-7-by-lat-by-lon array?
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일

0 개 추천

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

Pepe Grillo
Pepe Grillo 2021년 2월 25일
편집: Rik 2021년 2월 25일
Thanks all and Rik for the interaction!
it works only I change the line of u1(u1<0.1)=NaN becouse could be negatives velocities that means the water moves west. And should stays some NaNs that mean the coast line. I wouls check the data and let you know but looks great at the first.
Thanks!
Rik
Rik 2021년 2월 25일
That first line was only meant to generate random data. You should replace it with your actual data.
yes sure, just the second part works perfect
thk!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

질문:

2021년 2월 24일

댓글:

2021년 2월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by