필터 지우기
필터 지우기

Implicit expansion for griddedInterpolant

조회 수: 5 (최근 30일)
Manuel Deuerling
Manuel Deuerling 2023년 11월 9일
댓글: Bruno Luong 2023년 11월 9일
Hello,
I have to do a lot of interpolation on one data set and already found that griddedInterpolant is way faster than interp2;
It also allows element wise operation if two tensors of the same size are provided. As these are very big in my case, but repeat in some dimensions, I am wondering if something like implicit expansion (I hope I am using the correct terms here) can be used to speed up the code. Because for other functions using repmat is not advised.
Here I provide a minimum working example to show how I currently do it. Commented out are ways that I wish were possible to speed up the code but I can't get to run.
[X,Y] = ndgrid(0:10);
Z = rand([11,11]);
J = griddedInterpolant(X,Y,Z);
xq = sort(rand([4 1 3])*10);
yq = sort(rand([1 3 3])*10);
zq = J(repmat(xq,1,length(yq),1),repmat(yq,length(xq),1,1));
% zq = J(xq,yq); %implicit expansion?
% zq = bsxfun(@J,xq,yq);
% zq=J({xq,yq}) %Matt J's suggestion
If you have any input ( that can be generalized to different sizes of grids and lookups etc) I would be really thankful!
edit: changed xq, yq in the eaxmple to higher tensor to represent my problem more accurately

채택된 답변

Matt J
Matt J 2023년 11월 9일
Use the grid vector syntax of griddedInterpolant:
zq=J({xq,yq})
  댓글 수: 5
Manuel Deuerling
Manuel Deuerling 2023년 11월 9일
@Matt J Yeah thats true. To be even more specific in my current problem it is like 100x1x100x100 and 1x100x1x100 so I would expect it to be useful. But this might change. So I guess there just isnt a generic solution I wished for. Thanks for the input I will work with that!
@Bruno Luong This actually seems to be faster. Not quite what i hoped for, as programming does not seem to be so clean (especially with multiple dimensions etc). But thanks too!
Bruno Luong
Bruno Luong 2023년 11월 9일
Not too much messy with 4+D
zq = zeros(max(size(xq),size(yq)));
for k=1:size(zq(:,:,:),3)
zq(:,:,k) = J({squeeze(xq(:,:,k)),squeeze(yq(:,:,k))});
end

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

추가 답변 (1개)

Bruno Luong
Bruno Luong 2023년 11월 9일
  댓글 수: 2
Manuel Deuerling
Manuel Deuerling 2023년 11월 9일
Thanks for the suggestion & link.
If there is no other solution I will try this. I dont expect that an implementation by me could beat an efficient MathWorks algorithm in terms of memory/speed usage but I should probably just listen to you and try it.
Bruno Luong
Bruno Luong 2023년 11월 9일
It's probably depends on the size of your data. The strip down version like the one in this thread might beat TMW generic implementation.

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

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by