Split a numpy array into a similar array based on its contents

I have a numpy 2D array that represents the coordinates (x, y) of a curve, and I want to split this curve into pieces of the same length, getting the coordinates of the division points.

The simplest example is a string defined for two points, for example [[0,0], [1,1]] , and if I want to split it into two , the result will be [0,5,0,5] , and for three parts [[0.33,0.33], [0.67,0.67]] , etc.

How can I do this in a large array where the data is less simple? I am trying to split an array by length, but the results are not very good.

+4
source share
2 answers

If I understand well, then what you want is simple interpolation. For this, you can use scipy.interpolate( http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html ):

from scipy.interpolate import interp1d
f = interp1d(x, y) ## for linear interpolation
f2 = interp1d(x, y, kind='cubic') ## for cubic interpolation
xnew = np.linspace(x.min(), x.max(), num=41, endpoint=False)
ynew = f(xnew) ## or f2(xnew) for cubic interpolation

You can create a function that returns the coordinates of the divided points specified x, yand the number of desired points:

def split_curve(x, y, npts):
    from scipy.interpolate import interp1d
    f = interp1d(x, y)
    xnew = np.linspace(x.min(), x.max(), num=npts, endpoint=False)
    ynew = f(xnew)
    return zip(xnew[1:], ynew[1:])

For instance,

split_curve(np.array([0, 1]), np.array([0, 1]), 2) ## returns [(0.5, 0.5)]
split_curve(np.array([0, 1]), np.array([0, 1]), 3) ## [(0.33333333333333331, 0.33333333333333331), (0.66666666666666663, 0.66666666666666663)]

Note that x and y are numpy arrays, not lists.

+2
source

take the line length on each axis, split according to your desire.

Example: point 1: [0,0] point 2: [1,1]

then: the length of the line along the axes X: 1-0 = 1 also in the axes Y.

Now, if you want to split it into two parts, just split these lengths and create a new array.

[0,0], [. 5, 0.5], [1.1]

0
source

All Articles