# [SciPy-User] Interpolate between duplicate points for non monotonic array.

Dharhas Pothina Dharhas.Pothina@twdb.state.tx...
Tue Nov 23 08:41:11 CST 2010

```Thanks Josef, that got me pointed in the right direction. My solution:

x = np.array([1.0,1.0,1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,2.0,2.0,1.0,1.0,1.0,6.0])
xtmp = x.copy()
xtmp[1:] = x[1:] - x[:-1]
x_idx = np.nonzero(xtmp)[0]
x_vals = x[x_idx]
np.interp(np.arange(len(x)),x_idx, x_vals)

array([ 1.        ,  1.33333333,  1.66666667,  2.        ,  2.5       ,
3.        ,  3.33333333,  3.66666667,  4.        ,  3.        ,
2.        ,  1.5       ,  1.        ,  2.66666667,  4.33333333,  6.        ])

- d

>>> <josef.pktd@gmail.com> 11/22/2010 4:04 PM >>>
On Mon, Nov 22, 2010 at 4:53 PM, Dharhas Pothina
<Dharhas.Pothina@twdb.state.tx.us> wrote:
> Hi,
>
> A while back I asked a question about interpolating between duplicate point for a monotonic array.      I was able to get that working with the code below.
>
> x = np.array([1.0,1.0,1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,5.0,5.0,5.0,5.0,6.0,6.0])
> xUnique, xUniqueIndices = np.unique(x, return_index=True)
> idx = np.argsort(xUniqueIndices)
> np.interp(np.arange(len(x)), xUniqueIndices[idx], xUnique[idx])
>
> gives :
>
> np.array([ 1.        ,  1.33333333,  1.66666667,  2.        ,  2.5       ,
>                                                     3.        ,  3.33333333,  3.66666667,  4.        ,  4.5       ,
>                                                     5.        ,  5.25      ,  5.5       ,  5.75      ,  6.        ,  6.        ])
>
> now I need to do something similar for a non-monotonic array: i.e
>
> x = np.array([ 1.,  1.,  1.,  2.,  2.,  3.,  3.,  3.,  4.,  4.,  2.,  2.,  1., 1.,  1.,  6.])
>
> newx = np.array([1., 1.33, 1.67, 2., 2.5, 3., 3.33, 3.67, 4., 3., 2., 3., 4., 5., 6.])
>
> I've tried a few things but nothing has worked yet. as a corrolary question: is there a way to identify the indices on the array x that are the start of adjacent duplicate elements i.e a version np.unique that only uniques adjacent values.

just the last question

(np.diff(x) != 0)
np.nonzero(np.diff(x))

or some variation on it

Josef

>
> - dharhas
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
_______________________________________________
SciPy-User mailing list
SciPy-User@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

```