octave-bug-tracker
[Top][All Lists]

## [Octave-bug-tracker] [bug #36732] interp1 does not check input for monot

 From: Ben Abbott Subject: [Octave-bug-tracker] [bug #36732] interp1 does not check input for monotonicity Date: Tue, 21 Aug 2012 23:57:38 +0000 User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25

```Follow-up Comment #56, bug #36732 (project octave):

Regarding Michael's suggestion to use sort() in comment #52 in order to switch
form right to left-continous isn't so straight forward.  Consider the example
below, where yR will be right-continous by default.

X = [ 2 1 3 2];
Y = [ 2 1 3 10];
x = 1:0.5:3
yR = interp1 (X, Y, x);

This is equivalent to

yR = interp1 ([1 2 2 3], [1 2 10 3], x)

If sort(..., "descend") is applied to X and Y is modified accordingly we get
...

X = [ 2 1 3 2];
Y = [ 2 1 3 10];
[X, n] = sort (X, "descend")

Which results in

X =   3   2   2   1
n =   3   1   4   2
+verbatim

Using "n" to reorder "Y" results in;

Y = Y(n)

Y =    3    2   10    1

Notice that Y(2) and Y(3) are not what is intended.  Meaning, that the example
below still produces a right-continuous result.

X = [ 2 1 3 2];
Y = [ 2 1 3 10];
[~, n] = sort (X, "descend");
x = 1:0.5:3
yR = interp1 (X(n), Y(n), x);

In order the obtain a left-continuous result ...

X = [ 2 1 3 2];
Y = [ 2 1 3 10];
[~, n] = sort (flipud (X), "descend");
x = 1:0.5:3
yL = interp1 (X(n), fliplr(Y)(n), x)
yL =

3.0000   2.5000   2.0000   5.5000   1.0000

yR = interp1 (X, Y, x)
yR =

1.0000    1.5000   10.0000    6.5000    3.0000

My use of fliplr() may be misleading, since internally interp1() operates
along the rows and supports a 2D Y.  Meaning, the internal approach to doing
this is different from this example.

_______________________________________________________

<http://savannah.gnu.org/bugs/?36732>

_______________________________________________
Message sent via/by Savannah
http://savannah.gnu.org/

```