Vectorizing pairwise linear interpolation

7 vues (au cours des 30 derniers jours)
JPaquim
JPaquim le 21 Oct 2015
Commenté : JPaquim le 21 Oct 2015
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.

Réponse acceptée

Matt J
Matt J le 21 Oct 2015
Modifié(e) : Matt J le 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,[]).';
  1 commentaire
JPaquim
JPaquim le 21 Oct 2015
Thank you very much, that worked perfectly!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by