10

Does Python have a function like matlab's linspace in its standard library?

If not, is there an easy way to implement it without installing an external package?

Here's a quick and easy definition of linspace in matlab terms.

Note

I don't need a "vector result" as defined by the link, a list will do fine.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51

5 Answers5

14

The easiest way to implement this is a generator function:

from __future__ import division

def linspace(start, stop, n):
    if n == 1:
        yield stop
        return
    h = (stop - start) / (n - 1)
    for i in range(n):
        yield start + h * i

Example usage:

>>> list(linspace(1, 3, 5))
[1.0, 1.5, 2.0, 2.5, 3.0]

That said, you should definitely consider using NumPy, which provides the numpy.linspace() function and lots of other features to conveniently work with numerical arrays.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Smallest of a teeniest nitpick: `linspace(a, b, 1)` gives a `ZeroDivisionError`, while matlab linspace would return `b`. Oh, and `n` defaults to 100. – Martijn Pieters Sep 08 '12 at 21:09
  • 1
    @MartijnPieters: Valid comment. `numpy.linspace()` would return `a`, by the way. – Sven Marnach Sep 08 '12 at 21:10
  • 1
    From [the OP link](http://www.mathworks.se/help/matlab/ref/linspace.html): *"For n < 2, linspace returns b."*. That *could* be wrong, of course. :-P – Martijn Pieters Sep 08 '12 at 21:12
  • @MartijnPieters: Yes, I've noticed. I just found it amusing that the NumPy analogue does the opposite. :) – Sven Marnach Sep 08 '12 at 21:13
  • 1
    Me, on the other hand, had not read your comment closely enough, and misread `numpy` for matlib. :-) – Martijn Pieters Sep 08 '12 at 21:21
  • Another teeny nitpick: the last value of linspace(0, 1, 999) will not be exactly 1, because of normal numerical imprecision. A quick fix is to make the for loop over range(n - 1), followed by `yield stop`. – Warren Weckesser Sep 08 '12 at 22:30
  • @WarrenWeckesser: I'm aware of this and intentionally did not "fix" it. Floating point numbers are inexact by there very nature, and I see no point in trying to pretend that some of them can be exact. – Sven Marnach Sep 08 '12 at 23:01
13

No, it doesn't. You can write your own (whicn isn't difficult), but if you are using Python to fulfil some of matlab's functionality then you definitely want to install numpy, which has numpy.linspace.

You may find NumPy for Matlab users informative.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Katriel
  • 120,462
  • 19
  • 136
  • 170
8

You can define a function to do this:

def linspace(a, b, n=100):
    if n < 2:
        return b
    diff = (float(b) - a)/(n - 1)
    return [diff * i + a  for i in range(n)]
mkayala
  • 321
  • 1
  • 4
2

As long as the spacing is > 1, this is the Python equivalent to the following MATLAB function call: linspace(start,stop,spacing)

start = 5
stop = 6
spacing = int(11)
linspace = [start + float(x)/(spacing-1)*(stop-start) for x in range(spacing)]
ConnorB
  • 21
  • 2
0

Python 3 Solution using numpy

import numpy as np
    
x1 = np.linspace(-3,3,11)

Solution Rewrite using list comprehension

  1. How do we get 11 linear spaced numbers btw -3 & 3 ?

    x2 = [i for i in range(-5,6)]

  2. How do we scale these ints [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] down from -5 to 5 to -3 to 3 ?

    i/(5/3) 
    x2 = [ i for i/(5/3) in range(-5,6)]
    print(x2)
    
Gary Bao 鲍昱彤
  • 2,608
  • 20
  • 31