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

6 views (last 30 days)
Simone A. on 30 Apr 2023
Commented: dpb on 7 May 2023
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 CommentsShow 1 older commentHide 1 older comment
Mathieu NOE on 2 May 2023
maybe with some data it would help figure out a good solution ...

Image Analyst on 3 May 2023
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
dpb on 7 May 2023
"...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.

### Categories

Find more on Data Distribution Plots in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!