Nested Lists
| {list1,list2,...} | list of lists |
| Table[expr,{i,m},{j,n},...] | m×n×... table of values of expr |
| Array[f,{m,n,...}] | m×n×... array of values f[i, j, ...] |
| Normal[SparseArray[{{i1,j1,...}->v1,...},{m,n,...}]] |
| m×n×... array with element {is, js, ...} being vs |
| Outer[f,list1,list2,...] | generalized outer product with elements combined using f |
| Tuples[list,{m,n,...}] | all possible m×n×... arrays of elements from list |
Ways to construct nested lists.
This generates a table corresponding to a 2×3 nested list.
| Out[1]= |  |
|
This generates an array corresponding to the same nested list.
| Out[2]= |  |
|
Elements not explicitly specified in the sparse array are taken to be 0.
| Out[3]= |  |
|
Each element in the final list contains one element from each input list.
| Out[4]= |  |
|
Functions like
Array,
SparseArray and
Outer always generate
full arrays, in which all sublists at a particular level are the same length.
| Dimensions[list] | the dimensions of a full array |
| ArrayQ[list] | test whether all sublists at a given level are the same length |
| ArrayDepth[list] | the depth to which all sublists are the same length |
Functions for full arrays.
Mathematica can handle arbitrary nested lists. There is no need for the lists to form a full array. You can easily generate ragged arrays using
Table.
This generates a triangular array.
| Out[5]= |  |
|
| Flatten[list] | flatten out all levels of list |
| Flatten[list,n] | flatten out the top n levels |
| ArrayFlatten[list,rank] | create a flattened array from an array of arrays |
Flattening out sublists and subarrays.
This generates a 2×3 array.
| Out[6]= |  |
|
Flatten in effect puts elements in lexicographic order of their indices.
| Out[7]= |  |
|
This creates a matrix from a block matrix.
| Out[8]= |  |
|
| Transpose[list] | transpose the top two levels of list |
| Transpose[list,{n1,n2,...}] | put the kth level in list at level nk |
Transposing levels in nested lists.
This generates a 2×2×2 array.
| Out[9]= |  |
|
This permutes levels so that level 3 appears at level 1.
| Out[10]= |  |
|
This restores the original array.
| Out[11]= |  |
|
| Map[f,list,{n}] | map f across elements at level n |
| Apply[f,list,{n}] | apply f to the elements at level n |
| MapIndexed[f,list,{n}] | map f onto parts at level n and their indices |
Applying functions in nested lists.
This maps a function f at level 2.
| Out[13]= |  |
|
This applies the function at level 2.
| Out[14]= |  |
|
This applies f to both parts and their indices.
| Out[15]= |  |
|
| Partition[list,{n1,n2,...}] | partition into n1×n1×... blocks |
| PadLeft[list,{n1,n2,...}] | pad on the left to make an n1×n1×... array |
| PadRight[list,{n1,n2,...}] | pad on the right to make an n1×n1×... array |
| RotateLeft[list,{n1,n2,...}] | rotate nk places to the left at level k |
| RotateRight[list,{n1,n2,...}] | rotate nk places to the right at level k |
Operations on nested lists.
This rotates different amounts at each level.
| Out[17]= |  |
|
This pads with zeros to make a 2×3×3 array.
| Out[18]= |  |
|