49 views (last 30 days)

I have looked at the griddata function but am not clear if it applies to data on an irregular mesh. In my case, I have a 2d spatial dataset available on an [X,Y] grid where X and Y, which are the cartesian coordinates, are themselves 2d arrays. That is, both X and Y have sizes (m,n) but

X(:,1) ~= X(:,2) ~= X(:,3) and so on.

Similarly for the array Y. Visually, you can picturize the grid as follows, where X marks the location of the input data set:

X X X X

X X X X

X X X X

Along each row, the spacing between two Xs is uniform. But the spacing changes from row to row, leading to X(:,1), X(:,2), etc. all being different.

It looks like griddata will not accept such an input grid. Am I correct?

Thanks for your help.

Bjorn Gustavsson
on 25 Jun 2020

No you can use griddata and scatteredInterpolant. You can do something like this:

Zi = griddata(X(:),Y(:),Z(:),Xi,Yi);

And you do the same thing with scatteredInterpolant - the (:) construct just unwraps an array into a 1-D column array.

HTH

Bjorn Gustavsson
on 25 Jun 2020

Yes it does:

[X,Y] = meshgrid(1:12,1:11);

X(2:2:end,:) = X(2:2:end,:)+.5;

plot(X,Y,'+')

Z = peaks(12);

Z = Z(2:end,:);

subplot(1,2,1),

pcolor(X,Y,Z)

[Xi,Yi] = meshgrid(1:.1:12,1:.1:11);

Zi = griddata(X(:),Y(:),Z(:),Xi,Yi);

subplot(1,2,2)

pcolor(Xi,Yi,Zi),shading flat

You have to give the function the input it expects, for x, y and z. Then it demonstrably works.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_912706

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_912706

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_913360

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_913360

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_913408

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_913408

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_913741

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/554185-2d-interpolation-from-a-non-uniform-grid#comment_913741

Sign in to comment.