Interpolate to fill missing values in a temperature Matrix by considering the altitude (stored in a separate matrix)

Dear all,
I have got a matrix with temperatures (let's call it T), and a matrix with altitudes (let's call it Z). They're both 150x150.
T contains some missing values that I need to fill.
Rather than filling it with a basic linear interpolation, I need to fill it taking into account the altitude (Z).
Could you please suggest a code to fill the missing values in T weighted on Z?
Thanks a lot!

2 comentarios

With no more to go on than that, one would presume the location of the two already contains the variations in Z into account by position in the array. It wouldn't seem much else able to do other than interp2 around each missing location; unless, of course, the elevatioins in the other array are not representive of the location they occupy in the array -- in which case it wouldn't seem there would be sufficient data/knowledge available to do anything more, anyway, either.
maybe with some data it would help figure out a good solution ...

Iniciar sesión para comentar.

Respuestas (1)

What exactly does "missing" mean to you? You can't have "holes" in the matrix. The values must either be nan, 0, or some other value.
Did you try using interp2?
help interp2
INTERP2 2-D interpolation (table lookup). Vq = INTERP2(X,Y,V,Xq,Yq) interpolates to find Vq, the values of the underlying 2-D function V at the query points in matrices Xq and Yq. Matrices X and Y specify the points at which the data V is given. Xq can be a row vector, in which case it specifies a matrix with constant columns. Similarly, Yq can be a column vector and it specifies a matrix with constant rows. Vq = INTERP2(V,Xq,Yq) assumes X=1:N and Y=1:M where [M,N]=SIZE(V). Vq = INTERP2(V,K) returns the interpolated values on a refined grid formed by repeatedly halving the intervals K times in each dimension. This results in 2^K-1 interpolated points between sample values. Vq = INTERP2(V) is the same as INTERP2(V,1). Vq = INTERP2(...,METHOD) specifies alternate methods. The default is linear interpolation. Available methods are: 'nearest' - nearest neighbor interpolation 'linear' - bilinear interpolation 'spline' - spline interpolation 'cubic' - bicubic convolution interpolation for uniformly-spaced data. This method does not extrapolate and falls back to 'spline' interpolation for irregularly-spaced data. 'makima' - modified Akima cubic interpolation Vq = INTERP2(...,METHOD,EXTRAPVAL) specificies a method and a scalar value for Vq outside of the domain created by X and Y. Thus, Vq will equal EXTRAPVAL for any value of Yq or Xq which is not spanned by Y or X respectively. A method must be specified for EXTRAPVAL to be used, the default method is 'linear'. All the interpolation methods require that X and Y be monotonic and plaid (as if they were created using MESHGRID). If you provide two monotonic vectors, interp2 changes them to a plaid internally. X and Y can be non-uniformly spaced. For example, to generate a coarse approximation of PEAKS and interpolate over a finer mesh: [X,Y,V] = peaks(10); [Xq,Yq] = meshgrid(-3:.1:3,-3:.1:3); Vq = interp2(X,Y,V,Xq,Yq); mesh(Xq,Yq,Vq) Class support for inputs X, Y, V, Xq, Yq: float: double, single See also INTERP1, INTERP3, INTERPN, MESHGRID, griddedInterpolant, scatteredInterpolant. Documentation for interp2 doc interp2 Other uses of interp2 codistributed/interp2 gpuArray/interp2

5 comentarios

Hi @Image Analyst, thanks for your time. As you stated, the "missing values" are NaN values.
The thing with interp2, from what I understand, is that I can intrpolate to "replace" the missing values using the matrix itself as a reference (i.e., If I want to "fill" the NaN values in the T matrix, then I can use the non-NaN neighbouring pixels to approximate the value of NaN).
The thing is, that would interpolate values of temperatures at different elevations (please note, my ROI is a mountainous are, with peaks and valleys, thus with steep elevation changes).
For instance, my T matrix is:
T=[1, 5,1;
5,NaN,5;
1, 5,1];
and the associated Z matrix is:
Z=[300,100,300;
100,500,100;
300,100,300];
Now, if I interpolate the T matrix to replace the NaN value, NaN would become the interpolation of 5s and 1s. However, the altitute at the NaN coordinate would be different.
That's why I am trying to find a way to weight the interpolation of T based on the Z.
Anything you can think of? Thanks
if the relationship is monotonic betweenelements of T and Z , even they are presented as 2D arrays, the problem is simply a 1D interpolation, what I did below :
i modified a bit the data so I didn't have to deal with uniqueness (and have more unique data points)
% For instance, my T matrix is:
T=[1,4.95,1.2;
4.5,NaN,4.4;
1.3,5,1.4];
% and the associated Z matrix is:
Z=[300,110,290;
120,200,130;
250,100,220];
[Trow,ind] = sort(T(:));
idn = ~isnan(Trow);
Trow= Trow(idn);
Zrow= Z(:)
Zrow= Zrow(ind);
Zrow= Zrow(idn);
% sort Z elements in ascending order
[Zrow,ind] = sort(Zrow);
Trow= Trow(ind);
% plot relationship between T and Z
plot(Zrow,Trow);
% find interpolated value to replace NaN T element
T_nan = interp1(Zrow,Trow,Z(isnan(T)));
HI @Mathieu NOE , thanks a lot for your time. I am trying to apply that to my dataset but i run into that issue with unique values:
% Error using matlab.internal.math.interp1
% Sample points must be unique.
%
% Error in interp1 (line 188)
% VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);
Also, once I've obtained my T_nan, how can I replace the NaN values in my original matrix (T), with the T_nan values?
Sorry to bother you and thanks a lot for your help.
hello again
before you do the interpolation with interp1 , you have to make sure that the data (Zrow,Trow) are unique
use unique to remove duplicates
"...with interp1 , you have to make sure that the data (Zrow,Trow) are unique"
The functional also cannot be double-valued, so if your data follow the terrain up and down, that doesn't work, either.

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation of 2-D Selections in 3-D Grids en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Preguntada:

el 30 de Abr. de 2023

Comentada:

dpb
el 7 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by