Vectorizing pairwise linear interpolation

4 views (last 30 days)
Hey everyone,
I'm having some trouble vectorizing the code below for better performance:
for k = 1:n
intMat(k, :) = interp1(theta, data(:,:,k), angle_obs(k));
end
Basically, I have a 3D array of data, the first dimension corresponds to the variation with theta, the second dimension corresponds to the variation with frequency (not relevant here), and the third dimension corresponds to the various observations made. I need to interpolate the data of each observation with the corresponding observed angle (angle_obs), and get a matrix with the variation of theta for each observation.
My current implementation which performs a bit better is below:
auxMat = interp1(theta, data, angle_obs);
for k = 1:n
intMat(k, :) = auxMat(k, :, k);
end
But still, the interp1 function is calculating every pairwise interpolation, when all I need are the ones where the observation matches the respective angle_obs, so my for loop is keeping only the entries on the 1-3 "diagonal" of the array returned by interp1 (by the way, is there any way to vectorize this part of taking the diagonal entries?).
Is there anyway to vectorize what I intend to do? basically, use some sort of interp function that allows me to specify that I want it to calculate only the matching entries?
Thanks in advance
PS: I also posted this in the Newsgroup, don't know which place is more adequate, feel free to delete the inadequate one.

Accepted Answer

Matt J
Matt J on 21 Oct 2015
Edited: Matt J on 21 Oct 2015
m=size(data,2);
F=griddedInterpolant({theta,1:m,1:n},data);
[J,I]=ndgrid(1:m,1:n);
intMat=F([theta_obs(I(:)),J(:),I(:)]);
intMat=reshape(intMat,m,[]).';

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!