How replace NaN's in a 3d field with the nearest value in the same column?

I have a 3d (z,x,y) matrix and trying to fill NaN's in the each grid point in xy directions with the nearest value correspond to the previous layer (level).
Any help and information will be helpful. Thank you

2 Kommentare

Can you illustrate with a short example?
assume that have a 3d temperature matrix with 10 layers 'T(layer=10,x=30,y=50)', from the sea surface (layer = 1) to the seabed (layer=10) with NaN's values corresponding to the land. I am actually trying to replace the NaN's values at the seabed with the values of the previous layers.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 29 Mär. 2019
B = reshape(T,[],1);
lo = ~isnan(T);
ii = find(lo);
C = interp1(ii,B(lo),(1:numel(B))','previous');
T_out = reshape(C,size(T));

Weitere Antworten (3)

Farshid Daryabor
Farshid Daryabor am 29 Mär. 2019
assume that have a 3d temperature matrix with 10 layers 'T(layer=10,x=30,y=50)', from the sea surface (layer = 1) to the seabed (layer=10) with NaN's values corresponding to the land. I am actually trying to replace the NaN's values at the seabed with the values of the previous layers.
fillmissing(your3darray, 'previous', 3) %fill nans with previous element along 3rd dimension
Possibly, with an optional 'EndValues', 'next' if you want to fill NaNs on the 1st layer with the next non-Nan value.

3 Kommentare

unfortunately not exist function 'fillmissing' for the version I used.
Undefined function or variable 'fillmissing'.
fillmissing requires R2016b or later. There is a field up there in the right corner of the page where you can enter which release you're actually using so we don't have to guess which functions may or may not be available to you.

Melden Sie sich an, um zu kommentieren.

Farshid Daryabor
Farshid Daryabor am 3 Apr. 2019
I'm trying to index a double 2d matrix 'A' with a complex 2d matrix 'B' and I encountering with the following error message, Any help and information will be helpful to fix it. Thank you,
>> C=A(B);
Subscript indices must either be real positive integers or logicals.
>> whos A
Name Size Bytes Class Attributes
A 215x395 679400 double
>> whos B
Name Size Bytes Class Attributes
B 215x395 1358800 double complex

Kategorien

Mehr zu Oceanography and Hydrology finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by