Faster interpolation of 2-D spatial data at different time steps without looping through the time steps?
12 次查看(过去 30 天)
显示 更早的评论
I have a relatively small number of scattered data point locations (~30) which I want to interpolate onto a uniform spatial grid (e.g. 100 x 100). But the data set is also temporally varying, so I need to interpolate each spatial grid thousands (or tens of thousands) of times. Note, that I do not need to interpolate in time, I only need to interpolate spatially at each time step.
I am currently doing this with a for loop, but I am wondering if there is a way to do it faster in one step. See simple example below. Any help is appreciated!
%Create random data points:
xLoc = rand(30,1);
yLoc = rand(30,1);
%Create random data at each point varying temporally with 10000 points:
nT = 10000;
Data = randn(30,nT);
%uniform grid to interpolate onto:
xInt = 0:0.01:1;
yInt = 0:0.01:1;
[X,Y] = meshgrid(xInt,yInt);
%Create interpolant:
F = scatteredInterpolant(xLoc,yLoc,Data(:,1));
%Now loop over each time stamp. <<<--- This is the part I want to speed up
%This example current takes about 1.25 min on my machine, but I presume could
%be faster if I avoided a loop.
tic
V = nan(length(X(:)),nT);
for i = 1:size(Data,2)
F.Values = Data(:,i);
V(:,i) = F(X(:),Y(:));
end
toc
0 个评论
采纳的回答
Matt J
2023-1-16
Using func2mat from this FEX download,
%Create random data points:
xLoc = rand(30,1);
yLoc = rand(30,1);
%Create random data at each point varying temporally with 10000 points:
nT = 10000;
Data = randn(30,nT);
%uniform grid to interpolate onto:
xInt = 0:0.01:1;
yInt = 0:0.01:1;
[X,Y] = meshgrid(xInt,yInt);
%Create interpolant:
F = scatteredInterpolant(xLoc,yLoc,Data(:,1));
tic
A=func2mat(@(D) func(D,F,X,Y), Data(:,1) );
V=A*Data;
toc
function V=func(D,F,X,Y)
F.Values=D;
V=F(X(:),Y(:));
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!