Implicit expansion for griddedInterpolant

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

 Accepted Answer

Use the grid vector syntax of griddedInterpolant:
zq=J({xq,yq})

5 Comments

This would be perfect! Sorry, I overlooked this in the documentation-
However xq and yq might be tensors ( eg. size 4x1x3, and 1x3x3). Does this still work?
( I will be updating my example)
Just loop on top of it
[X,Y] = ndgrid(0:10);
Z = rand([11,11]);
J = griddedInterpolant(X,Y,Z);
xq = rand([4 1 3])*10;
yq = rand([1 3 3])*10;
zq = zeros(max(size(xq),size(yq)));
for k=1:size(zq,3)
zq(:,:,k) = J({squeeze(xq(:,:,k)),squeeze(yq(:,:,k))});
end
zq
zq =
zq(:,:,1) = 0.2847 0.4967 0.5708 0.4310 0.3534 0.5933 0.2953 0.5418 0.6691 0.2063 0.5544 0.3659 zq(:,:,2) = 0.6066 0.4763 0.8624 0.4667 0.6433 0.4922 0.4370 0.5760 0.4852 0.5364 0.2695 0.4939 zq(:,:,3) = 0.3236 0.6992 0.5391 0.4699 0.4195 0.4336 0.6179 0.2786 0.3980 0.2377 0.4675 0.3985
Matt J
Matt J on 9 Nov 2023
Edited: Matt J on 9 Nov 2023
@Manuel Deuerling Whether implicit expansion would help you depends on the dimensions of the tensors. For example, if you had size 2x1x3000, and 1x2x3000, there just wouldn't be any point pursuing a solution other than repmat. You basically have scattered query locations at that point.
@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!
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

Sign in to comment.

More Answers (1)

2 Comments

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.
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.

Sign in to comment.

Categories

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!