# Function to subdivide interval into n evenly-spaced points

[This post needs better tags than I could come up with. Edits to the tags would be particularly welcome.]

I realize that it is trivial to define a function that takes an interval (i.e. two endpoints, $a < b$), and an integer $n > 1$, and returns a list of $n$ evenly spaced points $x_1 = a, x_2, \dots, x_{n-1}, x_n = b$,1 but still, given that this functionality is frequently needed, and is commonplace in other scientific computing environments, I am surprised not to be able to find a Mathematica built-in for it in the docs.

Did I miss it?

(BTW: I'd love to lay my hand on some stash of such "useful functions that should be included-by-default in Mathematica but aren't".)

1 For example,

linearmesh[a_, b_, n_Integer /; n > 1] := Range[a, b, (b - a)/(n - 1)]
linearmesh[10, 20, 300] // N // Short
{10.,10.0334,10.0669,10.1003,<<292>>,19.8997,19.9331,19.9666,20.}


linearmesh2[a_, b_, n_Integer /; n > 0] := Range[a, b, (b - a)/n]
linearmesh2[10, 20, 300] // N // Short
{10.,10.0333,10.0667,10.1,10.1333,<<292>>,19.9,19.9333,19.9667,20.}


Etc.

I'm sure there are more clueful ways to implement this...

• 1) No, you didn't miss it -- it's called Range. If you frequently need certain convenience wrappers for Range, then put them in your initialization package. 2) As to a list of "useful functions that should be included-by-default in Mathematica but aren't", I suggest that what you would want to see on such a list would be very different from what I would want to see, and this would be true for any pair of users, making a universal version of such a list essentially impossible. The range of the interests of the user base is simply too broad. – m_goldberg Sep 20 '13 at 17:12
• Maybe FindDivisions? Depends on how exact you need to be. – chuy Sep 20 '13 at 18:28
• @m_goldberg, instead of pairwise comparison b/w users' desired functions, what about pooling all users and rank ordering preferences. Think of FAQs or even SE which relies on tagging and synonyms to relate similar questions. – alancalvitti Dec 16 '14 at 14:44

The three argument form of Array is convenient and I've used this a few times in different answers (example). The definition is quite simple:

linearmesh[a_, b_, n_Integer] := Array[# &, n, {a, b}]


and it gives you the same answers as your first example. Note that in your second example (with spacing $(b-a)/n$), you have 301 samples. This definition is used when you want 300 partitions, not 300 points.

• P.S. I thought this must have been asked before, but the only post I could find was a question on MATLAB's logspace. – rm -rf Sep 20 '13 at 18:05
• You can also use Identity instead of # &. – klimenkov Oct 9 '16 at 11:40

In version 10.1 the function Subdivide was introduced which does precisely that.

Subdivide
(* {0, 1/10, 1/5, 3/10, 2/5, 1/2, 3/5, 7/10, 4/5, 9/10, 1} *)

Subdivide[10, 5]
(* {0, 2, 4, 6, 8, 10} *)


Note that the number 5 equals the number of intervals not the amount of entries in the list (which is higher by one).

• Unfortunately, Subdivide is not very convenient when it comes to extremities which you have to handle manually (in contrast to Array). Array[# &, 1, {2, -2}] returns {0} while Subdivide[2, -2, 0] returns error. I could've settle with min or max or mean, but not with an error... – István Zachar Sep 20 '16 at 10:04

This function works exactly like MATLAB's linspace as it gives you n points (rather than n+1):

linspace[x0_, x1_, n_] := Range[x0, x1, (x1 - x0)/(n - 1)];

• How is this different from the OP's linearmesh? – Michael E2 Dec 16 '14 at 12:14

From the Manual https://reference.wolfram.com/language/ref/FindDivisions.html this can be easily solved:

FindDivisions[{1,10},9]

will produce {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} which are 10 in number though...

• The manual also says: "finds a list of about n "nice" numbers that divide the interval around to into equally spaced parts." The keyword here is "about". – Sjoerd C. de Vries Jul 1 '15 at 21:47