25
$\begingroup$

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?

$\endgroup$

marked as duplicate by rm -rf 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.

27
$\begingroup$

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.

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}

V<9

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

$\endgroup$
  • $\begingroup$ Nice, this one I´ll crib shamelessly :D $\endgroup$ – Yves Klett Nov 29 '13 at 12:29
  • $\begingroup$ I like Array, work like charm $\endgroup$ – Vibhor Singh Nov 29 '13 at 12:45
  • $\begingroup$ I've written Range[s, f, (f - s)/(n - 1)] so many times that my fingers run across the keyboard on their own $\endgroup$ – Dr. belisarius Nov 29 '13 at 20:32
  • 1
    $\begingroup$ Daaamn... I should've answered this question instead of the dupe. I answered the exact same question 3 months ago with the same answer and got only 2 votes :P $\endgroup$ – rm -rf Dec 1 '13 at 17:22
  • $\begingroup$ can you have a code example? $\endgroup$ – Charlie Parker May 1 '17 at 20:22
5
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ it is the way too, be careful with n=1. $\endgroup$ – Kuba Nov 29 '13 at 18:21
  • $\begingroup$ 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. $\endgroup$ – Peltio Nov 29 '13 at 18:24
  • $\begingroup$ I like this disclaimer :) but notice that I'm not reminding about negative numbers, strings etc. but about little function deficiency. $\endgroup$ – Kuba Nov 29 '13 at 18:36
  • $\begingroup$ 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 cens.ioc.ee/local/man/matlab/techdoc/ref/linspace.html is the first page that came up but it appears it is not on Mathworks servers. So my bad for not checking it thoroughly. $\endgroup$ – Peltio Nov 29 '13 at 18:41
  • $\begingroup$ it seems there is no good answer :) n=1 seems useless but on the other hand, why not? $\endgroup$ – Kuba Nov 29 '13 at 19:02

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