codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Python linspace limits from two arrays


By : , Category : python

Just use the first element than:

a = np.array((1, 2, 3, 4, 5))
b = np.array((2, 3, 4, 5, 6))
np.linspace(a[0], b[0], 11)

Result:

array([ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ])

EDIT

Just saw the comment. The OP wants a 2d array:

np.array([np.linspace(x, y, 11) for x, y in zip(a, b)])

array([[ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ],
       [ 2. ,  2.1,  2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ],
       [ 3. ,  3.1,  3.2,  3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ],
       [ 4. ,  4.1,  4.2,  4.3,  4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ],
       [ 5. ,  5.1,  5.2,  5.3,  5.4,  5.5,  5.6,  5.7,  5.8,  5.9,  6. ]])
ReLated :

What you are trying to do what won't work. First off you are allocating the memory on the stack WITHIN linspace with

double line[c];

You either new to allocate the memory before the call and pass it in, or allocate it dynamically and return it (and remember to free it later).

To allocate dynamically you can do the following:

double * line = new double[c];

Again, this will need to be freed like so at some point later when you are finished with it, otherwise you will have a memory leak.

delete line[];

Also double line[c]; creates an array of doubles and line points to that. So line is a double *. You specified the return type of the function as double

I´d suggest using meshgrid. Here is the documentation.


Example

>>> nx, ny = (3, 2)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> xv, yv = np.meshgrid(x, y)
>>> xv
array([[ 0. ,  0.5,  1. ],
       [ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]])

linspace args:

  1. Start value
  2. Final value
  3. Number of all values in the vector

You should not use the output of linspace to index an array! linspace generates floating point numbers and array indexing requires an integer.

From your code it looks like what you really want is something like this:

z_bin = numpy.linspace(0.0, 10.0, 21)

for i in range(len(z_bin)-1):
    zmin = z_bin[i]
    zmax = z_bin[i+1]

    # do some things with zmin/zmax

zmin and zmax will still be floating point (decimal) numbers.

You need you use floats when performing the division to calculate d.

For example:

(3- (-3))/(1000-1)=0
(3.-(-3.))/(1000.-1.)=0.006

So, in your function, you could :

def linspace(a,b,n):
    if n==1:
        yield b
        return
    d=float(b-a)/float(n-1)
    for i in range(n):
        yield a+d*i

Without casting to float, the first 10 values of your linspace are returned as:

[-3, -3, -3, -3, -3, -3, -3, -3, -3, -3]

But, if you use the function above:

[-3.0, -2.993993993993994, -2.987987987987988, -2.981981981981982, -2.975975975975976, -2.96996996996997, -2.963963963963964, -2.957957957957958, -2.951951951951952, -2.945945945945946]
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .