Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
hanciong awesome

Posts: 21
Registered: 1/11/12
interp2 can not handle matrix input??
Posted: May 4, 2013 9:53 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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

please help. thank you in advance.

hanciong



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.