Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Does Mathematica have an equivalent to Matlab's linspace? I want to make a list with "start", "stop" and "number of points". What is the neatest way of doing this?

share|improve this question

marked as duplicate by R. M. Dec 1 '13 at 17:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

There was an update for Array, not done to the end. The method below does not work for earlier versions even though that Array is

New in 1 | Last modified in 4

Moreover WRI forgot to update docs for error messages: Array::plen - the first example gives no error in V9.


Array[# &, n, {start, stop}] 
Array[# &, 10, {-1, 1}]
{-1, -(7/9), -(5/9), -(1/3), -(1/9), 1/9, 1/3, 5/9, 7/9, 1}


linespace[s_, f_, 1] := (f + s)/2
linespace[s_, f_, n_] := Range[s, f, (f - s)/(n - 1)]

share|improve this answer
Nice, this one I´ll crib shamelessly :D – Yves Klett Nov 29 '13 at 12:29
Is this new in v9? – ssch Nov 29 '13 at 12:30
@ssch In v9, Array is said to be last modyfied in v4. Also I think I've seen this syntax in Mr.Wizard's answers so in that case it was working in v7. – Kuba Nov 29 '13 at 12:31
I like Array, work like charm – Vibhor Singh Nov 29 '13 at 12:45
@Kuba when I try this, or equivalent examples on the Documentation site, I get error 'Array::plen: "{10} and {-1,1} should have the same length'. Examples where the 2nd and 3rd values are Lists of equal length work fine (e.g 'Array[f, {2, 3}, {{-1/2, 1/2}, {0, 1}}]' ). Any thoughts ? – PlaysDice Nov 29 '13 at 14:11

You could use table... unless I am missing something really basic. Speed, maybe? Edited to consider the special case as suggested by Kuba.

linspace[start_, stop_, n_:100] := Table[x, {x, start, stop, (stop - start)/(n - 1)}]
linspace[start_, stop_, 1] := Mean[{start,stop}]
share|improve this answer
it is the way too, be careful with n=1. – Kuba Nov 29 '13 at 18:21
Ah, ok. My procedures are written with the implicit disclaimer "this will help you aim at your foot, it's up to you not to pull the trigger". Well, it can be overloaded to consider the special case. – Peltio Nov 29 '13 at 18:24
I like this disclaimer :) but notice that I'm not reminding about negative numbers, strings etc. but about little function deficiency. – Kuba Nov 29 '13 at 18:36
I certainly appreciate feedback. So, basically, this special case could arise within some automated task that could compute the number of points and come up with the special, and apparently useless case n=1. Before writing my post I had a look at the web documentation for linspace for MATLAB and this special case is not mentioned. Actually, I see now this is the first page that came up but it appears it is not on Mathworks servers. So my bad for not checking it thoroughly. – Peltio Nov 29 '13 at 18:41
it seems there is no good answer :) n=1 seems useless but on the other hand, why not? – Kuba Nov 29 '13 at 19:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.