5
$\begingroup$

Is there an efficient way to output a collection of equally spaced points in Mathematica, in arbitrary dimensions?

In 1D there is the Range function, which given two endpoints and a spacing, outputs the numbers between those endpoints, spaced by that spacing, starting from the smallest endpoint. I'm wondering whether this can be generalized to multiple dimensions efficiently, for example, if one would like to produce a grid of equally spaced points in the square $[0,1]^2$ with spacing $1/2$, the output should be {{0,0},{0,1/2},{0,1},{1/2,0},{1/2,1/2},{1/2,1},{1,0},{1,1/2},{1,1}}.

$\endgroup$
1
  • 3
    $\begingroup$ Tuples[Range[smallest,largest,spacing],dimensions] $\endgroup$
    – Bill
    Aug 13 at 16:29

2 Answers 2

8
$\begingroup$
Table[{i, j, k}, {i, 0, 1, 1/2}, {j, 0, 1, 1/2}, {k, 0, 1, 1/2}]
Array[List, {3, 3, 3}, {{0, 1}, {0, 1}, {0, 1}}]
Outer[List, Range[0, 1, 1/2], Range[0, 1, 1/2], Range[0, 1, 1/2]]
Tuples[{Range[0, 1, 1/2], Range[0, 1, 1/2], Range[0, 1, 1/2]}]
Tuples[Range[0, 1, 1/2], 3]

I extended to three dimensions to highlight the patterns. Also, the title mentioned "regular grid", so I included examples that don't produce the "flat" output.

Table looks to be the most difficult to extend/modify, but it's the most explicit. The second version of Tuples is the easiest to extend, but requires that all dimensions have the same scale. Note also that Tuples produces the "flat" output requested. Outer and the first Tuples are a nice balance of flexibility and simplicity.

Edit

The above was accepted as the answer, but I feel compelled to note that Syed's solution is really the most semantically aligned with the way the problem was posed. Even though it's been around awhile, I wasn't aware of CoordinateBoundsArray, and it will definitely come in handy for me. I'll record Syed's suggestion here so it gets attention as the accepted answer.

CoordinateBoundsArray[{{0, 1}, {0, 1}, {0, 1}}, 1/2]

(You can, of course, flatten the result.)

$\endgroup$
6
$\begingroup$

You could also explore the many variations of CoordinateBoundsArray from the docs. For your particular example:

Flatten[CoordinateBoundsArray[{{0, 1}, {0, 1}}, 1/2], 1]
{{0, 0}, {0, 1/2}, {0, 1}, {1/2, 0}, {1/2, 1/2}, {1/2, 1}, {1, 0}, {1,
   1/2}, {1, 1}}
$\endgroup$
3
  • 1
    $\begingroup$ Nice! Learned a new one! $\endgroup$
    – lericr
    Aug 13 at 17:37
  • $\begingroup$ I wish there were some sort of community approved answer, because I'd definitely vote for your solution. $\endgroup$
    – lericr
    Aug 14 at 20:14
  • $\begingroup$ @lericr The structure of Mathematica is such that you can have multiple valid answers to a question. Building things from the ground up is satisfying but a built-in solution can be utilized to reduce maintenance costs. Here I note that all solutions in Mathematica are not created equal in terms of memory/speed/update frequency etc. Thanks for your kind words and for appending this solution to your answer. $\endgroup$
    – Syed
    Aug 15 at 4:13

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.