Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: interp2 can not handle matrix input??
Replies: 8   Last Post: May 6, 2013 4:11 PM

 Messages: [ Previous | Next ]
 hanciong awesome Posts: 21 Registered: 1/11/12
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./2-y./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.....

hanciong

Date Subject Author
5/4/13 hanciong awesome
5/4/13 Bruno Luong
5/4/13 hanciong awesome
5/4/13 Bruno Luong
5/4/13 hanciong awesome
5/4/13 dpb
5/4/13 hanciong awesome
5/4/13 dpb
5/6/13 Steven Lord