# Faster interpolation of 2-D spatial data at different time steps without looping through the time steps?

2 views (last 30 days)
Darcy Cordell on 16 Jan 2023
Commented: Darcy Cordell on 16 Jan 2023
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

Matt J on 16 Jan 2023
%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
Elapsed time is 0.558389 seconds.
function V=func(D,F,X,Y)
F.Values=D;
V=F(X(:),Y(:));
end
Darcy Cordell on 16 Jan 2023
This results in an amazing speed-up thanks.