Create linear spaced vector?

Discussion in 'Python' started by kjm, Dec 17, 2004.

  1. kjm

    kjm Guest

    Hi Everyone,

    I am trying to port some old MatLab code to python, and am stuck on
    how to accomplish something.

    I am trying to write a generalized function that will create a
    linearly spaced vector, given the start and end point, and the number
    of entries wanted.

    In MatLab I have this function that I wrote:

    Code:
    
    function out = linearspace(x1,x2,n)
    
    out = [x1+ (0:n-2)*(x2 - x1)/(floor(n)-1) x2];
    
    return
    
    
    

    I have the numeric package, numarray installed, and I think it should
    be accomplished easily, but I just can't seem to get the syntax
    correct with python.

    Any tips would be greatly appreciated.


    Thanks
     
    kjm, Dec 17, 2004
    #1
    1. Advertising

  2. On 2004-12-17, kjm <> wrote:

    > I am trying to write a generalized function that will create a
    > linearly spaced vector, given the start and end point, and the number
    > of entries wanted.


    >>> from scipy import *
    >>> mgrid[0.0:10.0:5j]

    array([ 0. , 2.5, 5. , 7.5, 10. ])

    --
    Grant Edwards grante Yow! Is this where people
    at are HOT and NICE and they
    visi.com give you TOAST for FREE??
     
    Grant Edwards, Dec 17, 2004
    #2
    1. Advertising

  3. kjm

    John Hunter Guest

    >>>>> "kjm" == kjm <> writes:

    kjm> Hi Everyone, I am trying to port some old MatLab code to
    kjm> python, and am stuck on how to accomplish something.

    kjm> I am trying to write a generalized function that will create
    kjm> a linearly spaced vector, given the start and end point, and
    kjm> the number of entries wanted.

    kjm> In MatLab I have this function that I wrote:

    kjm>
    Code:
    
        kjm> function out = linearspace(x1,x2,n)
    
    in matlab the builtin function to accomplish this is "linspace"
    
    The python package matplotlib defines a host of matlab compatible
    functions, including linspace
    
    def linspace(xmin, xmax, N):
       if N==1: return xmax
       dx = (xmax-xmin)/(N-1)
       return xmin + dx*arange(N)
    
    
    Note that matplotlib extends the Numeric/numarray core of matlab
    compatible functions (defined in MLab) to include plotting functions
    
      http://matplotlib.sourceforge.net
    
    A listing of matlab compatible functions is provided at
    http://matplotlib.sourceforge.net/matplotlib.pylab.html
    
    
    JDH
     
    John Hunter, Dec 17, 2004
    #3
  4. On Fri, 2004-12-17 at 13:39, kjm wrote:
    > Hi Everyone,
    >
    > I am trying to port some old MatLab code to python, and am stuck on
    > how to accomplish something.
    >
    > I am trying to write a generalized function that will create a
    > linearly spaced vector, given the start and end point, and the number
    > of entries wanted.
    >
    > In MatLab I have this function that I wrote:
    >
    >
    Code:
    > 
    > function out = linearspace(x1,x2,n)
    > 
    > out = [x1+ (0:n-2)*(x2 - x1)/(floor(n)-1) x2];
    > 
    > return
    > 
    > 
    > 
    >
    >
    > I have the numeric package, numarray installed, and I think it should
    > be accomplished easily, but I just can't seem to get the syntax
    > correct with python.
    >
    > Any tips would be greatly appreciated.
    >
    >
    > Thanks


    Is this want you want?

    #!/usr/bin/python

    def linear_space( start, end, count ):

    """ Returns a vector containing count evently spaced intervals
    (count + 1 evenly spaced points) """

    delta = (end-start) / float(count)
    return [start,] + \
    map( lambda x:delta*x + start, range( 1, count ) ) + [end, ]

    if __name__ == "__main__":
    print linear_space( 1.0, 2.0, 10 )

    Running it gives you:
    [1.0, 1.1000000000000001, 1.2, 1.3, 1.3999999999999999, 1.5,
    1.6000000000000001, 1.7000000000000002, 1.8, 1.8999999999999999, 2.0]



    Adam DePrince
     
    Adam DePrince, Dec 17, 2004
    #4
  5. kjm

    Guest

    Thanks for the code snippets guys. Exactly what I needed to get going.

    I knew I could get the solution from matplotlib, but getting it
    installed using Fink (OS X) has been giving me a headache, so I thought
    I could just write my own function for now to get a small piece of code
    written....

    The help is greatly appreciated.

    kjm

    John Hunter wrote:
    > >>>>> "kjm" == kjm <> writes:

    >
    > kjm> Hi Everyone, I am trying to port some old MatLab code to
    > kjm> python, and am stuck on how to accomplish something.
    >
    > kjm> I am trying to write a generalized function that will create
    > kjm> a linearly spaced vector, given the start and end point, and
    > kjm> the number of entries wanted.
    >
    > kjm> In MatLab I have this function that I wrote:
    >
    > kjm>
    Code:
    >
    >     kjm> function out = linearspace(x1,x2,n)
    >
    > in matlab the builtin function to accomplish this is "linspace"
    >
    > The python package matplotlib defines a host of matlab compatible
    > functions, including linspace
    >
    > def linspace(xmin, xmax, N):
    >    if N==1: return xmax
    >    dx = (xmax-xmin)/(N-1)
    >    return xmin + dx*arange(N)
    >
    >
    > Note that matplotlib extends the Numeric/numarray core of matlab
    > compatible functions (defined in MLab) to include plotting functions
    >
    >   http://matplotlib.sourceforge.net
    >
    > A listing of matlab compatible functions is provided at
    > http://matplotlib.sourceforge.net/matplotlib.pylab.html
    > 
    > 
    > JDH[/color]
     
    , Dec 17, 2004
    #5
  6. kjm

    John Hunter Guest

    >>>>> "kjmacken" == kjmacken <> writes:

    kjmacken> Thanks for the code snippets guys. Exactly what I
    kjmacken> needed to get going. I knew I could get the solution
    kjmacken> from matplotlib, but getting it installed using Fink (OS
    kjmacken> X) has been giving me a headache, so I thought I could
    kjmacken> just write my own function for now to get a small piece
    kjmacken> of code written....

    Yes, matplotlib fink installs have frustrated many an OSX user. Note
    that the matplotlib.mlab package (where linspace and others functions
    reside) do not require any extension code and can be reused anywhere
    you like as python code by copying and pasting, etc.

    Also, Robert Kern is in the process of building an "enthon" package
    for OSX that has most of the utilities for scientific computing
    including matplotlib built-in. Batteries included on steroids,
    basically.

    http://www.scipy.org/wikis/featurerequests/MacEnthon

    So keep your eyes on that site for release information.

    JDH
     
    John Hunter, Dec 17, 2004
    #6
  7. kjm

    Guest

    John,

    Thanks for the heads up RE: scipy, I will keep my eyes on the
    developments.

    Also, thanks for the info:

    I was able to get these modules into my site-packages directory, and
    make use of what is there.

    Cheers,

    kjm
     
    , Dec 17, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    HTML seems double-spaced

    VB Programmer, Jun 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,706
    Kevin Spencer
    Jun 9, 2004
  2. bad_knee
    Replies:
    0
    Views:
    541
    bad_knee
    Jul 13, 2003
  3. bad_knee
    Replies:
    2
    Views:
    568
    bad_knee
    Jul 14, 2003
  4. Replies:
    1
    Views:
    1,212
    Roedy Green
    Nov 15, 2005
  5. Replies:
    8
    Views:
    1,996
    Csaba
    Feb 18, 2006
Loading...

Share This Page