How to |
Work with Nested Lists
Nested lists are lists within a list; they are the principal structure for data in Mathematica
and allow for high-dimension arrays and ragged datasets as well as common uses such as matrices.
Create a list of lists to work with:
Matrices in Mathematica
are represented as nested lists. Note that each row corresponds to a sublist in the nested list:
, the short form of the Part
function, to get the second row:
From row 2, get element 3:
Get element 3 from each row:
to remove the nesting:
Display the flattened data as a column:
For comparison, to display the flattened data as a row, add
You can operate on the individual sublists of a nested list or on the nested list as a whole.
Most functions map over each sublist within a nested list.
Create four plots from the rows in the dataset:
Most descriptive statistics functions operate by columns.
Get the mean of all the numbers by flattening the list:
Create a doubly nested list:
now lists the means by each individual nested sublist:
to obtain the mean of the entire dataset: