Looping scatteredInterpolant across each frame in 3D matrix

I am trying to perform a 2D interpolation across each frame in the 3rd dimension in a 3D matrix.
I have:
xgrid: 759x551
ygrid: 759x551
zgrid: 759x551x523
I am hoping to interpolate each frame of the zgrid but scatteredInterpolant does not allow indexing nor the use of cell arrays.
My first step is to use meshgrid to create the 2D resolution I desire, I end up with:
xgrid_q: 301x201
ygrid_q: 301x201
My question is, how do I interpolate the zgrid into 301x201x523?

4 Comments

Since your data is gridded, why aren't you using griddedInterpolant?
If we've misunderstood your input data, I would advise attaching xgrid, ygrid, xgrid_q, ygrid_q in a .mat file (all four in one single file) so we can see what it really is.
I would really like to but even if I take a subset of the data, it is too large to attach.
My data is gridded but it is not a regular rectangular grid. I am hoping to interpolate it onto a regular grid. When I try to use griddedInterpolant, I get an error that the grid arrays must have NDGRID structure.
Basically, I want to treat each frame of zgrid as a 3D interpolation, and loop that through each frame, interpolating each frame onto a regular rectangular grid. For some reason, scatteredInterpolant does not allow looping like that.
My data is gridded but it is not a regular rectangular grid. I am hoping to interpolate it onto a regular grid. When I try to use griddedInterpolant, I get an error that the grid arrays must have NDGRID structure.
If your data is gridded, it should have NDGRID structure. That's what it means to be gridded.
Please shows us the plot you get when you do this,
scatter(xgrid(:),ygrid(:))
zooming in as necessary so we can see how the xgrid(i),ygrid(i) pairs are spaced apart.
Yeah I think the image will help you see that it is gridded but not rectangular gridding. I am hoping to interpolate it onto a grid that is regular (y = 2 4 6 8....; x = 2, 4, 6, 8...)

Sign in to comment.

 Accepted Answer

Using the variable names given on the scatteredInterpolant documentation page, do you mean that your xgrid variable is the x input, ygrid is the y input, and zgrid is actually the v input, the values of the function you want to interpolate at the points in xgrid and ygrid? So you're not actually changing the grid each time, you're changing the values to try to interpolate 523 different data sets one at a time using the same object?
If that's what you're trying to do see the "Replacement of Sample Values" on the documentation page I linked above. Just change the Values property of the scatteredInterpolant object to reference a different page of the zgrid variable each time you want to interpolate. This allows the object to continue using the same triangulation it built when it was originally constructed, which is a lot of the work involved in creating the object.
Or if you're trying to do gridded interpolation, the griddedInterpolant object makes this even a little easier. As stated in the Version History section of that documentation page, as of release R2021a you can "specify a 2-D grid, a 3-D array of values at the grid points, and a 2-D collection of query points, then griddedInterpolant returns the interpolated values at the query points for each 2-D page in the 3-D array of values." See the "Interpolate Multiple Sets of Values on Same Grid" example on the griddedInterpolant documentation page I linked above.

More Answers (1)

Since your grids are related by an affine warping, it might be best to use imwarp, assuming you have the appropriate toolbox.Example:
zgrid(:,:,1) = imread('cameraman.tif');
zgrid(:,:,2) = imresize(im2gray(imread('peppers.png')),[256,256]);
montage(zgrid)
tform = affine2d([1 0 0; .5 1 0; 0 0 1]);
Zgrid = imwarp(zgrid,tform);
montage(Zgrid)

Categories

Products

Release

R2022b

Asked:

on 31 May 2023

Edited:

on 1 Jun 2023

Community Treasure Hunt

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

Start Hunting!