Wolfram: Nested List, Array, Tree, Dimensions
Nested List: Array, Matrix, Tree
List is critically important in WolframLang. Here's some notion on tree, array, you need to understand.
- Matrix (aka rectangular array, n-dimensional array)
-
A nested list where there are same number of elements at each level of nesting. e.g.
{{3,1}, {4, 5}, {2, 4}}. - Tree
-
(aka jagged array) A arbitrarily nested list. e.g.
{3, {4, 5}, {9}}.
Dimensions and ArrayDepth
Dimensions[expr]-
Shape of rectangular array. A list of integers that is the count at each level of nesting.
e.g. a 3 by 2 matrix has dimensions
{3,2}.Dimensions[{a, b}] (* {2} *) Dimensions[{x, y, z}] (* {3} *) Dimensions[{{a, b}, {c, d}}] (* {2, 2} *) Dimensions[{{{a}, {b}}, {{c}, {d}}}] (* {2, 2, 1} *) (* jagged array. return the max regularity *) Dimensions[{{{a}, {b}}, {{c}, d}}] (* {2, 2} *)
ArrayDepth[expr]-
same as
Length[Dimensions[expr]]typically known as the “dimension” of a array.
(* 2 by 2 matrix *) Dimensions[{{a,b},{c,d}}] (* {2, 2} *) ArrayDepth[{{a,b},{c,d}}] (* 2 *) (* s------------------------------ *) (* 2 by 3 matrix *) Dimensions[{{a,b,c},{d,e,f}}] (* {2, 3} *) ArrayDepth[{{a,b,c},{d,e,f}}] (* 2 *) (* s------------------------------ *) (* creat a 2 by 2 by 2 matrix *) Table[ f[x,y,z], {x,1,2},{y,1,2},{z,1,2}] (* { { {f[1, 1, 1], f[1, 1, 2]}, {f[1, 2, 1], f[1, 2, 2]}}, { {f[2, 1, 1], f[2, 1, 2]}, {f[2, 2, 1], f[2, 2, 2]} } } *) Dimensions @ Table[ f[x,y,z], {x,1,2},{y,1,2},{z,1,2}] (* {2, 2, 2} *) ArrayDepth @ Table[ f[x,y,z], {x,1,2},{y,1,2},{z,1,2}] (* 3 *)