Vectorizing pairwise linear interpolation

11 visualizaciones (últimos 30 días)
JPaquim
JPaquim el 21 de Oct. de 2015
Comentada: JPaquim el 21 de Oct. de 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.

Respuesta aceptada

Matt J
Matt J el 21 de Oct. de 2015
Editada: Matt J el 21 de Oct. de 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 comentario
JPaquim
JPaquim el 21 de Oct. de 2015
Thank you very much, that worked perfectly!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by