user2738748
user2738748

Reputation: 1116

How to interpolate scattered data in matab?

I have two-dimensional data in Matlab. The X values and the Y values aren't distributed uniformly, so I can't use the interp2 function.

As far as I understand, I should use the griddata function. Here is how I do it:

%//x_val  - my x values, of size, lets say, 32 by 1;
%//y_val - my y values,  of size, let's say, 128 by 1;
%//f_val - values of my function, of size 32 by 128.
%//[x,y] - the point in which I want to get the interpolated value of my function.

result = griddata(x_val,y_val,f_val,x,y);

However, I get the following error:

Error using griddata (line 109)

The lengths of X and Y must match the size of Z.

It seems to me that I'm using the interpolation in 3D (griddata(x,y,z,v,xq,yq,zq)), instead of 2D (griddata(x,y,v,xq,yq)), but I don't know what I'm doing wrong.

What is the correct way of using griddata? What's wrong with my code?

I have looked over the examples, however, I can't figure out how they relate to my issue.

Upvotes: 0

Views: 746

Answers (1)

Suever
Suever

Reputation: 65430

Based on your question, you have data on a non-uniform grid; however, griddata expects a unique x and y value for every value of the function (f_val). So you'll need to use ndgrid to create these unique combinations of x and y.

[xx,yy] = ndgrid(x_val, y_val);
result = griddata(xx(:), yy(:), f_val(:), x, y)

Upvotes: 1

Related Questions