Find Z-value corresponding to X and Y points based on interpolated data

7 Ansichten (letzte 30 Tage)
Lai Ping
Lai Ping am 6 Jan. 2023
Beantwortet: Karim am 6 Jan. 2023
I would like to perform 2D interpolation for scatter data that is supported by Matlab Coder package.
I have a set of data, with x,y and z cordinates, in the following format.
X Y Z
1 2 10
2 2 8
2 4 9
3 2 9
3 4 10
3 6 9
So at Z(1,2) = 10, Z(2,2) = 8 and so on.
I wish to find the value of Z(1,3), Z(3,1) for example and eventually output the code to C/C++ using Matlab Coder package.
I tried using griddata and scatterInterpolant which is what I wish to achieve, however, it is not supported by Coder package. So I tried to work with interp2 or gridedInterpolant, however I am not sure how to convert my sample data to full grided form.
Is there any way to convert the above data to be used ny interp2 or gridedInterpolant?
Or is there other function that I can use and I wasn't aware of that?
Appreciate for any help! Thanks!
  2 Kommentare
VBBV
VBBV am 6 Jan. 2023
Read about ndgrid or meshgrid functions.
Lai Ping
Lai Ping am 6 Jan. 2023
I tried and I get error, sample points must be unique.
Here is my code, I am not sure where I did made mistake. Does it matter if I have duplicated value in x vector and y vector?
x=[1,2,2,3,3,3];
y=[2,2,4,2,4,6];
z=[10,8,9,9,10,9
10,8,9,9,10,9
10,8,9,9,10,9
10,8,9,9,10,9
10,8,9,9,10,9
10,8,9,9,10,9];
xCor=0:3;
yCor=0:3;
[xx,yy] = meshgrid(x,y);
zz = interp2(xx,yy,z, xCor, yCor)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Karim
Karim am 6 Jan. 2023
Note that you have very little points. Hence one possibility is to perform linear interpolation, below you can see one method on how to do this.
% original data by OP
x = [1 2 2 3 3 3]';
y = [2 2 4 2 4 6]';
z = [10 8 9 9 10 9]';
% plot the points in 3D space
figure
scatter3(x,y,z,'r','filled')
grid on; view([-70 30]);
title('Original points')
% set up the grid for the interpolation
x_fine = linspace(min(x),max(x),20);
y_fine = linspace(min(y),max(y),20);
[Xq,Yq] = meshgrid(x_fine,y_fine);
% create a linear fitting trough the points
sfun = fit([x, y],z,'linearinterp');
Zq = feval(sfun,Xq,Yq);
% plot the results
figure
surf(Xq,Yq,Zq)
grid on; view([-70 30])
title('Surface through interpolated points')
figure
scatter3(Xq,Yq,Zq,'g','filled')
grid on; view([-70 30])
title('Interpolated points')

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by