7
$\begingroup$

I have two ranges of values, one from 0 to 20 and the other from 0 to 80. I want to combine these into a list of "all possible" {x, y} coordinates that I can then plot later on. So the final list of points would be ~1700 long, consisting of:

{{0,0}, {0,1}, {0,2},..., {0,80}, {1,0}, {1,2}, {1,3},..., {1,80}, {2,0},..., {20, 80}}

I've seen things like Riffle and Partition thrown around but they're not quite what I'm looking for I think.

$\endgroup$
1
  • 1
    $\begingroup$ This is the Cartesian or outer product. $\endgroup$ Commented Dec 7, 2012 at 7:38

3 Answers 3

12
$\begingroup$

The best aproach uses Tuples :

Tuples[{Range[0, 80], Range[0, 20]}] // Short 
{{0, 0}, {0, 1}, {0, 2}, {0, 3}, <<1693>>, {80, 17}, {80, 18}, {80, 19}, {80, 20}}

or a bit more briefly :

Tuples[ Range @@@ {{0, 80}, {0, 20}}]

Array provides a different way :

Sequence @@@ Array[{#1, #2} &, {81, 21}, 0]
$\endgroup$
3
  • $\begingroup$ Haha, I seriously just figured it the second before I hit refresh to see if there were any responses before I deleted this. And that's exactly what I typed! Thanks anyway. :P $\endgroup$
    – SixtySuit
    Commented Dec 7, 2012 at 2:46
  • $\begingroup$ @SixtySuit Oh, really ? Take this comparison into account : mathematica.stackexchange.com/questions/4748/… $\endgroup$
    – Artes
    Commented Dec 7, 2012 at 9:42
  • 1
    $\begingroup$ One can simplify the last method a little: Sequence @@@ Array[ List , {81, 21}, 0] $\endgroup$
    – evanb
    Commented Nov 23, 2016 at 14:04
7
$\begingroup$

Shorter versions of Artes' methods:

Tuples @ Range[0, {80, 20}]

Join @@ Array[List, {81, 21}, 0]

Related methods that may be of use:

a = {1, 2, 3, 4};
b = {q, r, s};

Distribute[{a, b}, List]
{{1, q}, {1, r}, {1, s}, {2, q}, {2, r}, {2, s}, {3, q},
 {3, r}, {3, s}, {4, q}, {4, r}, {4, s}}
Inner[f, List /@ a, {b}, g]
{{g[f[1, q]], g[f[1, r]], g[f[1, s]]}, {g[f[2, q]], g[f[2, r]], 
  g[f[2, s]]}, {g[f[3, q]], g[f[3, r]], g[f[3, s]]}, {g[f[4, q]], 
  g[f[4, r]], g[f[4, s]]}}
Outer[f, a, b]
{{f[1, q], f[1, r], f[1, s]}, {f[2, q], f[2, r], f[2, s]}, {f[3, q], 
  f[3, r], f[3, s]}, {f[4, q], f[4, r], f[4, s]}}

Notice the different levels preserved and opportunities to apply custom functions in each case. All methods shown have a place.

Also be familiar with the syntax of Table which is closely related to Do, Sum, Product, etc.

Table[{i, j}, {i, a}, {j, b}]
{{{1, q}, {1, r}, {1, s}}, {{2, q}, {2, r}, {2, s}}, {{3, q},
  {3, r}, {3, s}}, {{4, q}, {4, r}, {4, s}}}
Product[i + j, {i, a}, {j, b}]
(1 + q) (2 + q) (3 + q) (4 + q) (1 + r) (2 + r) (3 + r) (4 + r) (1 + s) (2 + s) (3 + s) (4 + s)

Also related is:

KroneckerProduct[a, b]
{{q, r, s}, {2 q, 2 r, 2 s}, {3 q, 3 r, 3 s}, {4 q, 4 r, 4 s}}
$\endgroup$
7
$\begingroup$

A more general function is Outer

 tuples = Outer[ List, Range[0, 80], Range[0, 20]]

Depending on how you want to use it you might want to Flatten the output

 Flatten[ tuples, 1]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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