How to interpolate a set of data wher the X size is different for the Y size

Hello,
I have a set of data X (size=2001,1) and Y (size=3,1). The Z matrix size is (2001,3). X and Y have uniform increments by the X size is not the same as the size of Y.
Can I use meshgrid or interp2? I tried but I got errors
for meshgrid
Error using griddata
The lengths of X and Y must match the size of Z.
for interp2
Error using griddedInterpolant
Sample points vector corresponding to grid dimension 1 must contain 3 elements.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 126)
F = makegriddedinterp({X, Y}, V, method,extrap);
Error in read_mydata (line 41)
Vq = interp2(theta,phi,co_amp_db,10,22);
Error in read_mydata (line 40)
vq = griddata(theta,phi,co_amp_db,10,22)
=======================================
Here is my code
xlx=input_data_file;
sheetname='MAIN';
addpath(pathToOptimizer)
nrow=2002;
ncol=13;
rangero=[1,1,nrow,ncol];
sheetname='Test';
[NUM,TXT,RAW]=readmycells(xlx,rangero,sheetname);
phi=NUM(1,3:4:ncol)';
theta=NUM(2:nrow,1);
nnp=size(phi);
nnt=size(theta);
np=nnp(2);
nt=nnt(1);
co_amp_db=NUM(2:nrow,2:4:ncol);
co_phase_db=NUM(2:nrow,3:4:ncol);
cx_amp_db=NUM(2:nrow,4:4:ncol);
cx_phase_db=NUM(2:nrow,5:4:ncol);
vq = griddata(theta,phi,co_amp_db,10,22);
Vq = interp2(theta,phi,co_amp_db,10,22);

 Accepted Answer

x=1:10;y=1:3; % coarse spacing
z=rand(numel(x),numel(y));
surf(y,x.',z)
hold on
Z=interp2(y,x.',z,1:0.5:3,[1:0.5:10].'); % increase spacing by half...
surf(1:0.5:3,[1:0.5:10].',Z); % plot on top...

4 Comments

Thank, it works but I am supposed to create a function to be called by an integral,.i.e. Z should be a value for a set of (x0,y0) to be later provided to the calling integration function
so for x0=2.5,y0=.65 I tried
Z=interp2(y,x.',z,x0,y0) where I was expecting Z to be one value.
I get Z=NaN.
Y=[1:14].';% 1 x 14
X=[1:2:5].'; % 1 x 3
Z=rand(14,3); % 14 x 3
[xq,yq]=meshgrid(1:0.0005:5,1:0.0005:14); %
%step % step higher/finer spacing to include
%all or any query points of interest
interp2(X,Y,Z,xq,yq)
The reason why you get NaN values is related to the step size selected, in the interp2 function, So, if you have non uniform query points such as x0=2.5,y0=.65, e.g. 0.65 which does not exist or get created by meshgrid, the resulting output wil be NaN. So you need to use a very fine spacing in meshgrid function, which includes your query points of interest.
Check also scatteredInterpolant or griddedInterpolant

Sign in to comment.

More Answers (1)

you can usw meshgrid to prepare your xq and yq:
Y=9:13;X=1:2:5;Z=rand(5,3);
[xq,yq]=meshgrid(1:0.5:5,9:0.5:13);
interp2(X,Y,Z,xq,yq)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 21 Jan 2023

Commented:

on 22 Jan 2023

Community Treasure Hunt

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

Start Hunting!