I am new to Julia and noticed the following when playing around with list comprehensions:
Given some integers a
, b
,c
, the comprehension
[[i,j] for i=1:a for j=1:b]
returns an a*b \times 1
array while
[[i,j] for i=1:a, j=1:b]
returns an a \times b
array of pairs.
Similarly for three generators,
[[i,j,k] for i=1:a for j=1:b for k=1:c]
returns an a*b*c \times 1
array while
[[i,j] for i=1:a, j=1:b, k=1:c]
returns an a \times b \times c
array.
This lead me to believe that I could determine the shape of the “outer array” that results from the above kind of comprehensions by connecting the generators (which is how I call the expressions ‘i=1:a’ etc.) by “,” and “for” in the right way. For instance,
[[i,j,k] for i=1:a, j=1:b for k=1:c]
would return a a \times b*c
array and
[[i,j,k] for i=1:a, j=1:b for k=1:c]
a a*b \times c
dimensional array, etc.
But this is not the case. Instead any call involving more than one “for” expression returns a vector, with the ordering of the elements depending on the position of the “for” expressions.
This brings up two questions:
- What is the simplest way of generating the kinds of arrays that I would have assumed the above calls generate?
- Is there a simple rule for determining what kind of vector (or array) a specific combination of “for” and “,” will generate under the actual syntax?
Thanks!