# Array of uniformly spaced numbers

I wanted to create a grid of equally spaced numbers. So far I obtained this:

m = Table[{i, j}, {i, 0.0, 1.0, 0.5}, {j, 0.0, 1.0, 0.5}]


Now when I am trying to cast them as vectors I obtained:

{ {{0., 0.}, {0., 0.5}, {0., 1.}},
{{0.5, 0.}, {0.5, 0.5},{0.5, 1.}},
{{1., 0.}, {1., 0.5}, {1., 1.}} }


Which, instead, I would like to be something like:

{{0., 0.},
{0., 0.5},
{0., 1.},
{0.5, 0.},
{0.5, 0.5},
{0.5, 1.},
...., }

• perhaps Partition is what you're looking for – user42582 Sep 14 '17 at 12:33
• What is the question? – Peter Mortensen Sep 14 '17 at 21:35
• You simply wanna join your sublists: m =Join@@ Table[{i, j}, {i, 0.0, 1.0, 0.5}, {j, 0.0, 1.0, 0.5}] – Fortsaint Oct 13 '17 at 17:52

You can convert the full array into a list of pairs using Join, Catenate, or Flatten:

Join @@ m
Catenate[m]     (* version 10 or later *)
Flatten[m, 1]


(@@ is shorthand for Apply. See also Partition and ArrayReshape.)

However there are arguably better ways to approach this problem from the outset than using Table. Knowing that you want a "flat" output of pairs I would turn to Tuples. The uniformly spaced numbers can be generated with Range, which is Listable, allowing this for your example:

Tuples @ Range[0, {1, 1}, .5]

{{0., 0.}, {0., 0.5}, {0., 1.}, {0.5, 0.}, {0.5, 0.5},
{0.5, 1.}, {1., 0.}, {1., 0.5}, {1., 1.}}


Generalized to ranges that do not share common values, in two equivalent forms

Tuples @ Range[{0, 7}, {1, 8}, {.5, 1}]

Tuples[{Range[0, 1, .5], Range[7, 8, 1]}]

{{0., 7}, {0., 8}, {0.5, 7}, {0.5, 8}, {1., 7}, {1., 8}}

{{0., 7}, {0., 8}, {0.5, 7}, {0.5, 8}, {1., 7}, {1., 8}}


If you did want a full array instead of a flat list of pairs then look at Array and CoordinateBoundsArray:

Array[List, {3, 3}, {{0, 1}}]

CoordinateBoundsArray[{{0, 1}, {0, 1}}, 0.5]


The syntax of Array is unlike the others in that the number of steps is specified rather than the size of the steps; this is sometimes more convenient.

Each of these generalized to the second example given for Tuples:

Array[List, {3, 2}, {{0, 1}, {7, 8}}]

CoordinateBoundsArray[{{0, 1}, {7, 8}}, {0.5, 1}]

{{{0., 7}, {0., 8}}, {{0.5, 7}, {0.5, 8}}, {{1., 7}, {1., 8}}}

{{{0., 7}, {0., 8}}, {{0.5, 7}, {0.5, 8}}, {{1., 7}, {1., 8}}}


I'd use Outer, and then Flatten to get the desired level:

Flatten[Outer[List, Range[0, 1, 0.5], Range[0, 1, 0.5]], 1]

{{0., 0.}, {0., 0.5}, {0., 1.}, {0.5, 0.}, {0.5, 0.5},
{0.5, 1.}, {1., 0.}, {1., 0.5}, {1., 1.}}

• Would you really? Why not Tuples? I'm all for alternatives (i.e. thanks for posting this), but practically I find Tuples superior here. – Mr.Wizard Sep 14 '17 at 15:15
• You are probably right... but I always find Outer and Inner products to be concrete feeling, and I like that you can tell them which operations to use when combining. I guess when the operation is List, there are many other possibilities. – bill s Sep 14 '17 at 19:15
• I can understand that perspective. By the way to do you know that Tuples works on any head? Tuples[foo[{a, b, c}, h[1, 2, 3]]] – Mr.Wizard Sep 14 '17 at 22:28
• One man's Tuples is another man's Outer: Flatten[Outer[foo, {a, b, c}, {1, 2, 3}], 1] == Tuples[foo[{a, b, c}, h[1, 2, 3]]] returns True. It's kind of amazing that h just disappears. – bill s Sep 15 '17 at 0:52

A variation on the method given by bill s:

Range[0, 1, 0.5] // Distribute[{#, #}, List] &


% // MatrixForm


$$\left( \begin{array}{cc} 0. & 0. \\ 0. & 0.5 \\ 0. & 1. \\ 0.5 & 0. \\ 0.5 & 0.5 \\ 0.5 & 1. \\ 1. & 0. \\ 1. & 0.5 \\ 1. & 1. \\ \end{array} \right)$$