2
$\begingroup$

How do I create an array a of generic depth $n+1$, such that exactly:

a[[i1]][[i2]]...[in]] = {i1,i2,...,in} ?

In words, such that the last-level arrays are arrays telling exactly their position in the previous levels?

Thanks.

$\endgroup$
  • $\begingroup$ @Kuba wow, much more elegant than my solution. $\endgroup$ – yohbs Oct 20 '15 at 10:41
5
$\begingroup$

Like this?

Table[{i, j, k, l}, {i, 4}, {j, 2}, {k, 4}, {l, 3}]

If dimList is a list of the desired dimenions then you can automate this via

makeArray[dimList_List] := Table[
  Array[x, Length[dimList]],
  Evaluate[Sequence @@ Transpose@{Array[x, Length[dimList]], dimList}]
]

-------Edit-------------

Kuba's solution in the comments is much more elegant:

Array[List,dimList]
$\endgroup$
  • $\begingroup$ Yes...but the depth is unknown and unfixed. How do I make it work for every depth? $\endgroup$ – geodude Oct 20 '15 at 10:31
  • $\begingroup$ Yup! Thank you. $\endgroup$ – geodude Oct 20 '15 at 10:36
  • $\begingroup$ I edited to make the structure a little more transparent $\endgroup$ – yohbs Oct 20 '15 at 10:37

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.