Vectorizing pairwise linear interpolation

조회 수: 10 (최근 30일)
JPaquim
JPaquim 2015년 10월 21일
댓글: JPaquim 2015년 10월 21일
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.

채택된 답변

Matt J
Matt J 2015년 10월 21일
편집: Matt J 2015년 10월 21일
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
JPaquim
JPaquim 2015년 10월 21일
Thank you very much, that worked perfectly!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by