bugGNU Octave - Bugs: bug #49506, interp2(...,'linear') gives...

 
 

bug #49506: interp2(...,'linear') gives incorrect result

Submitter:  Muhali <muhali>
Submitted:  Tue 01 Nov 2016 02:04:45 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 07 Nov 2016 12:44:03 AM UTC, comment #7: 

I checked something in along the lines that Nir suggested (http://hg.savannah.gnu.org/hgweb/octave/rev/125391edc71b).  Fixed, closing report.

Rik <rik5>
Group administrator
Sun 06 Nov 2016 11:47:22 AM UTC, comment #6: 

Senseless undocumented Matlab behavior.

As a work-around,  we can replace line 228 in scripts/general/interp2.m


ZI = a(idx) + b(idx).*Xsc + c(idx).*Ysc + d(idx).*Xsc.*Ysc;


with

fis = @(x, i) reshape(x(i), size(i));
ZI = fis(a,idx) + fis(b,idx).*Xsc + fis(c,idx).*Ysc + fis(d,idx).*Xsc.*Ysc;


Then we can check that

%!assert (interp2 ([2 3], [2 3 4], [1 2; 3 4; 5 6], [2 3], 3, "linear"), [3 4])


Nir Krakauer <nir_krakauer>
Sun 06 Nov 2016 02:57:35 AM UTC, comment #5: 

I asked on the Octave Maintainer's list and the behavior of indexing with a vector is required for Matlab compatibility.

Rik <rik5>
Group administrator
Wed 02 Nov 2016 03:28:35 PM UTC, comment #4: 

Is this something we are following for Matlab compatibility?  For scalars or matrices, the resulting value after indexing has the size of the idx vector.  It is only when a vector is indexed by another vector that the result follows the original vector's size.

Matlab test


x = [1 2 3];
x([3 2 1]')
x'([3 2 1])



Rik <rik5>
Group administrator
Wed 02 Nov 2016 12:40:37 PM UTC, comment #3: 

The problem is caused by how vector indexing is working in Octave. If a is 2x1, while idx is 1x2, is there any reason that a(idx) is 2x1? Seems like it should be 1x2. (If a was a matrix instead of a vector, a(idx) would indeed be 1x2.)

Nir Krakauer <nir_krakauer>
Tue 01 Nov 2016 09:38:08 PM UTC, comment #2: 

The problem is in the final line of the "linear" method.

## apply plane equation
ZI = a(idx) + b(idx).*Xsc + c(idx).*Ysc + d(idx).*Xsc.*Ysc;


At this point the factors a, b, c, d are all column vectors (2x1), while Xsc and Ysc are row vectors (1x2).  The multiplication steps broadcast automatically to create 2x2 final matrices.

I don't know what the best way to resolve this is.

Rik <rik5>
Group administrator
Tue 01 Nov 2016 06:04:27 PM UTC, comment #1: 

Confirmed.  Maybe something with automatic broadcasting is going on.

Rik <rik5>
Group administrator
Tue 01 Nov 2016 02:04:45 PM UTC, original submission:  

try


x = [2 3] ; y = [2 3 4] ;
f = [1 2; 3 4; 5 6] ;
xi = [2 3] ; yi = 3 ;
interp2(x, y, f, xi, yi, 'linear')


and you get

ans =

   3   4
   3   4

With any other method one gets the correct result [3 4].

Muhali <muhali>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by nir_krakauer (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by muhali (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-11-07 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2016-11-01 rik5 StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code