
interp2 can not handle matrix input??
Posted:
May 4, 2013 9:53 AM


Hello all,
I have a problem. Apparently when I use interp2 with matrices as inputs, I get strange results. Try to run this code:
%%% BEGINNING OF CODE %%%%%%%%%
clear all
% Making vector interpolation of x and y xcb = linspace(0,5,50); ycb = linspace(0,5,40);
% Making grid of x and y [xgr,ygr] = ndgrid(xcb,ycb);
% Define a function fu = @(x,y) exp(x./2y./2);
% Making grid of z zgr = fu(xgr,ygr);
% Making interpolation function of fu(x,y) fuint = @(x,y) interp2(xgr',ygr',zgr',x,y,'cubic',NaN);
% Use matrices as inputs: ytry = [1,2;3,4]; xtry = [1,1;1,1];
disp('====================') disp(ytry)
% The result of using matrices as input: disp(fuint(xtry,ytry))
result = zeros(size(ytry)); for kr = 1:numel(result) result(kr) = fuint(xtry(kr),ytry(kr)); end
% The result of using element by element calculation disp(result)
%%%%%%%%%%% END OF CODE %%%%%%%%%%
as you can see, the result is different. I try this in matlab 2012b. This problem doesn't happen if the inputs are row or column vectors. strange.....
please help. thank you in advance.
hanciong

