Run interpolation for several datasets at once

13 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2022-1-25
It might be that they are simmilar but that depends heavily on the case which is modeled so shouldn't be taken into account too much
So, for example, the spacing between x(m,n,i) and x(m+1,n,i) is not constant with i?
DS
DS 2022-1-25
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).

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2022-1-25
编辑:Matt J 2022-1-25
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 个评论
DS
DS 2022-1-28
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

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2022-1-25
编辑:Matt J 2022-1-25
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.

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by