Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
interp2 can not handle matrix input??

Subject: interp2 can not handle matrix input??

From: hanciong

Date: 4 May, 2013 13:53:08

Message: 1 of 9

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

Subject: interp2 can not handle matrix input??

From: Bruno Luong

Date: 4 May, 2013 13:59:11

Message: 2 of 9

>
> as you can see, the result is different. I try this in matlab 2012b.

I can't see. What I get is this:

====================
     1 2
     3 4

    0.3679 0.2231
    0.1353 0.0821

    0.3679 0.2231
    0.1353 0.0821

It seems alright to me

Bruno

Subject: interp2 can not handle matrix input??

From: hanciong

Date: 4 May, 2013 14:13:09

Message: 3 of 9

what version of matlab you are using? because in matlab 2012b, there is such problem. I have tried it in matlab 2012a, there is no problem. If you want I can print screen and show it to you.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <km347f$5tt$1@newscl01ah.mathworks.com>...
> >
> > as you can see, the result is different. I try this in matlab 2012b.
>
> I can't see. What I get is this:
>
> ====================
> 1 2
> 3 4
>
> 0.3679 0.2231
> 0.1353 0.0821
>
> 0.3679 0.2231
> 0.1353 0.0821
>
> It seems alright to me
>
> Bruno

Subject: interp2 can not handle matrix input??

From: Bruno Luong

Date: 4 May, 2013 14:28:10

Message: 4 of 9

I'm using 2013a at home and 2012a at work (we skip two upgrades due to notable quality drop of the new versions).

Bruno

Subject: interp2 can not handle matrix input??

From: dpb

Date: 4 May, 2013 14:38:54

Message: 5 of 9

On 5/4/2013 8:59 AM, Bruno Luong wrote:
>>
>> as you can see, the result is different. I try this in matlab 2012b.
>
> I can't see. What I get is this:
>
> ====================
> 1 2
> 3 4
>
> 0.3679 0.2231
> 0.1353 0.0821
>
> 0.3679 0.2231
> 0.1353 0.0821
>
> It seems alright to me

Well, again I'm rushed but here

...
====================
      1 2
      3 4
     0.3679 0.3679
     0.1353 0.1353

     0.3679 0.2231
     0.1353 0.0821
 >>

First column same; second isn't.

 >> ver
-----------------------------------
MATLAB Version: 8.0.0.783 (R2012b)
...

I recall was some discussion re: interpX here relatively recently re:
features being removed and that some areas were a little funky in what
had been added as extensions. I know at that time was before had
received the R2012b release and there were differences in how my release
(R12) handled some and didn't have some of the added cases that are, by
reading the doc, confusing at best in their description.

I also recall there being one problem in interp1 array inputs in early
release including R12 where if number of interpolants was greater than
number of terms in the table (columns>rows, iow), the order of
interpolation was swapped owing to the internal use of length() in the
function instead of size(x,1). Whether something like that has happened
again in a particular release I have no idea but on this release here,
at least, I seem to see OP's complaint...no time to delve into what's
going on at the moment, though, this has to be just a "drive by", sorry...

--

Subject: interp2 can not handle matrix input??

From: hanciong

Date: 4 May, 2013 14:43:09

Message: 6 of 9

Hi Bruno,

this is the screenshot from 2012a: https://dl.dropboxusercontent.com/u/93494026/Screenshot%202012a.png

and this is the screenshot from 2012b: https://dl.dropboxusercontent.com/u/93494026/Screenshot%202012b.png

Pay attention to the versions number that I circle with red on the top of the image. Also the results in command window. They are different.

hanciong

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <km35tq$a59$1@newscl01ah.mathworks.com>...
> I'm using 2013a at home and 2012a at work (we skip two upgrades due to notable quality drop of the new versions).
>
> Bruno

Subject: interp2 can not handle matrix input??

From: hanciong

Date: 4 May, 2013 14:44:09

Message: 7 of 9

Hi dpb,

The problem only exists in 2012b, exactly the version you are using. In matlab 2012a, there is no such problem.

dpb <none@non.net> wrote in message <km36h5$g8$1@speranza.aioe.org>...

> Well, again I'm rushed but here
>
> ...
> ====================
> 1 2
> 3 4
> 0.3679 0.3679
> 0.1353 0.1353
>
> 0.3679 0.2231
> 0.1353 0.0821
> >>
>
> First column same; second isn't.
>
> >> ver
> -----------------------------------
> MATLAB Version: 8.0.0.783 (R2012b)
> ...
>
> I recall was some discussion re: interpX here relatively recently re:
> features being removed and that some areas were a little funky in what
> had been added as extensions. I know at that time was before had
> received the R2012b release and there were differences in how my release
> (R12) handled some and didn't have some of the added cases that are, by
> reading the doc, confusing at best in their description.
>
> I also recall there being one problem in interp1 array inputs in early
> release including R12 where if number of interpolants was greater than
> number of terms in the table (columns>rows, iow), the order of
> interpolation was swapped owing to the internal use of length() in the
> function instead of size(x,1). Whether something like that has happened
> again in a particular release I have no idea but on this release here,
> at least, I seem to see OP's complaint...no time to delve into what's
> going on at the moment, though, this has to be just a "drive by", sorry...
>
> --

Subject: interp2 can not handle matrix input??

From: dpb

Date: 4 May, 2013 17:15:29

Message: 8 of 9

On 5/4/2013 9:44 AM, hanciong wrote:

...[don't toppost--hard conversation follow makes]...

> The problem only exists in 2012b, exactly the version you are using. In
> matlab 2012a, there is no such problem.
...

Well, interp2 is an m-file; you can use debug and step through and see
where it goes south. I again don't have the time just now to look at in
depth.

Or, if it isn't next release check for patches and/or upgrades for which
you are eligible. Probably worth at least a notice to official TMW
support at mathworks.com; maybe they have a workaround already.

--

Subject: interp2 can not handle matrix input??

From: Steven_Lord

Date: 6 May, 2013 20:11:30

Message: 9 of 9



"hanciong " <hanciong@gmail.com> wrote in message
news:km36rp$ch8$1@newscl01ah.mathworks.com...
> Hi dpb,
>
> The problem only exists in 2012b, exactly the version you are using. In
> matlab 2012a, there is no such problem.

I did some investigation and I believe this is a manifestation of bug
886257:

http://www.mathworks.com/support/bugreports/details.html?rp=886257

This bug has been fixed in release R2013a. For release R2012b, it worked as
expected when I changed your function to:

fuint = @(x,y) reshape(interp2(xgr',ygr',zgr',x(:),y(:),'cubic',NaN),
size(x));

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us