4
$\begingroup$

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.},
 ...., }
$\endgroup$
  • 1
    $\begingroup$ perhaps Partition is what you're looking for $\endgroup$ – user42582 Sep 14 '17 at 12:33
  • $\begingroup$ What is the question? $\endgroup$ – Peter Mortensen Sep 14 '17 at 21:35
  • 1
    $\begingroup$ 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}] $\endgroup$ – Fortsaint Oct 13 '17 at 17:52
10
$\begingroup$

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}}}

$\endgroup$
4
$\begingroup$

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.}}
$\endgroup$
  • $\begingroup$ Would you really? Why not Tuples? I'm all for alternatives (i.e. thanks for posting this), but practically I find Tuples superior here. $\endgroup$ – Mr.Wizard Sep 14 '17 at 15:15
  • $\begingroup$ 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. $\endgroup$ – bill s Sep 14 '17 at 19:15
  • 1
    $\begingroup$ 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]]] $\endgroup$ – Mr.Wizard Sep 14 '17 at 22:28
  • $\begingroup$ 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. $\endgroup$ – bill s Sep 15 '17 at 0:52
2
$\begingroup$

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)$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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