Run interpolation for several datasets at once

3 views (last 30 days)
DS
DS on 25 Jan 2022
Commented: DS on 28 Jan 2022
Hello,
My code is performing interpolation of gridded data. My Sample values "V" are stored within an 3D array.
X and Y values are given within corresponding 3D arrays. The third dimension stores basically the different datasets (there are N different cases).
e.g.
X(:, :, 1) Y(:, :, 1) V(:, :, 1) -> Dataset 1
X(:, :, 2) Y(:, :, 2) V(:, :, 2) -> Dataset 2 (...)
The points I want to interpolate my data to (xglobal, yglobal) are constant for all Datasets.
In the end the Interpolated data is stored again within an 3D Array.
for i=1:N
x_Wake = x(:,:,i);
y_Wake = y(:,:,i);
V = Vi(:,:,i);
globaldata = griddata(x_Wake, y_Wake, Vi, xglobal, yglobal);
matV(:,:,i) = V;
end
This is part of a rountine so this step is time critical - which is the reason why I want to get rid of the loop and
Interpolate all datasets at once. I tried using griddedinterpolant - but I couldn't make it work.
Thanks in advance for any help/ ideas provided!
Best wishes
DS
  5 Comments
DS
DS on 25 Jan 2022
ah sorry - thats what you meant!
x and y used to be both vectors with constant spacing (x and y have their own spacing constant). which have been meshed.
Nevertheless they're different from dataset to dataset (the spacing remains constant).

Sign in to comment.

Accepted Answer

Matt J
Matt J on 25 Jan 2022
Edited: Matt J on 25 Jan 2022
x and y used to be both vectors with constant spacing (x and y have their own spacing constant). which have been meshed.
Let's forget the mesh. I will now pretend x and y are the original vectors, and likewise I will assume xglobal and yglobal are grid vectors. There is never any reason in Matlab to mesh vectors purely for the purposes of interpolation.
Nevertheless they're different from dataset to dataset (the spacing remains constant).
If so, then there is some translation vector [tx(i),ty(i)] such that for each i x and y can be written
x=x0+tx(i);
y=y0+ty(i);
Then, the interpolation loop can be done more efficiently as,
F=griddedInterpolant({x0,y0,1:N},Vi);
L=length(xglobal)
[XY,Z]=ndgrid([xglobal(:);yglobal(:)],1:N);
X=XY(1:L,:)-tx(:).';
Y=XY(l+1:end,:)-ty(:).';
matV= reshape( F([X(:),Y(:),Z(:)]) , size(Vi));
  1 Comment
DS
DS on 28 Jan 2022
Thank you for your commitment!
Your first comment helped me to get this point 300xfaster.
Then I discovered some cases where my vectors stopped being uniform- your second comment helped me as a basis to get the job done.
I have to say - nice community around here. I'm new to programming so that was really instructive information for me.
Thanks a bunch.
DS

Sign in to comment.

More Answers (1)

Matt J
Matt J on 25 Jan 2022
Edited: Matt J on 25 Jan 2022
If it's gridded interpolation, you shouldn't be using griddata. You should be using interp2.
for i=1:N
x_Wake = x(:,:,i);
y_Wake = y(:,:,i);
V = Vi(:,:,i);
globaldata = interp2(x_Wake, y_Wake, Vi, xglobal, yglobal);
end
It would also speed things up if you have x_Wake and y_Wake be grid vectors instead of a full grid.

Community Treasure Hunt

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

Start Hunting!

Translated by