# Lists, Tables, and Matrices¶

Often, we would like to do more than just store individual values, particularly when we start wanting to analyze experimental data. As such, we would like a way to analyze datasets all at once (finding the mean, standard deviation, etc.), and may want to do even more. We’ll now take a look at some of the ways we can do this in Mathematica.

## Lists¶

Lists are a convenient way of storing linear data. For example, we might want to store the results of several runs of an experiment (in Modern Lab, maybe the Gamma-Gamma experiment with total number of photons captured in a given time interval). We can do this fairly simply:

data={21, 24, 25, 32, 31, 22, 18}


To access the elements of data, we apply the indexing operator:

data[[1]]
data[[1;;2]]
data[[2;;4]]
data[[{1,3,5}]]

prints

21
{21, 24}
{24, 25, 32}
{21, 25, 31}


We can perform operations on lists as well, many of which can be extremely powerful:

{1, 2, 3, 4, 5}^2
Length[{4}] (*Length of list*)
Total[{1,2,3,4,5}] (*Sum of all elements*)
Mean[{0,1,2}] (*Arithmetic average of list*)
Norm[{1,1,1,1}] (*The norm of the list - think the norm of a vector*)
Dot[{0,1},{1,0}] (*Dot product of the given vectors*)
Union[{1,2,3},{3,4,5}] (*Union of the two sets*)
{0, 2, 4} + {4, 2, 0} (*Vector addition*)

prints

{1, 4, 9, 16, 25}
1
15
1
2
0
{1, 2, 3, 4, 5}
{4, 4, 4}


The above examples show some useful functions on lists. Applying a simple arithmetic operators on scalars applies the operation to each element and returns that list. Many desired functions often exist within Mathematica without having to re-create them by hand: Length is just the number of elements in the list, Mean is the arithmetic average of the elements, Norm is the mathematical norm $$||list||$$, Dot takes the the dot (inner) product of the two lists, and Union treats the lists as sets and takes the union. Many other useful functions exist (many can be found here). The last example above shows that the addition operator for lists can also be applied to add each $$i^\textrm{th}$$ element and return the resultant list.

## Tables¶

Tables are just lists created by the Table command which is easy to use and effective for producing sequences:

Table[i,{i,1,10}]
Table[i^2,{i,5}]
Table[i, {i, {3, 2, 4}}]
list = {2, 3, 4};
Table[list[[i]]^2, {i, Length[list]}]
Table[{i, list[[i]]^2}, {i, Length[list]}]


is

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 4, 9, 16, 25}
{3, 2, 4}
{4, 9, 16}
{{1, 4}, {2, 9}, {3, 16}}


Essentially, Table accepts some function over the domain given and will then create the list of the elements in the sequence over the given elements in the domain. The second example shows that given {i, N}, the domain is $$i\in[1, N]$$. The third example shows that the elements to use can be given explicitly as well. The last examples combine what we learned about lists above to show how easily different concepts and structures in Mathematica can be combined.

## Matrices¶

Just as tables are actually just lists, lists are in fact matrices:

{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}


is the matrix $$\left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right)$$ in the same way that

{{1, 2, 3}}


is the row vector $$\left(\begin{array}{ccc}1 & 2 & 3\end{array}\right)$$ and

{{1}, {2}, {3}}


is the column vector $$\left(\begin{array}{c}1\\2\\3\end{array}\right)$$.

We can take selections of the data with indices as normal

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
mat[[{1, 3}, 2;;3]]

gives

{{2, 3}, {8, 9}}


But we can also do transformations with All which selects all the possible values:

mat[[1, All]]


gives

{1, 2, 3}


We have many operations on matrices at our disposal too. For example:

m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Transpose[m]
Det[m]
m . m
MatrixPower[m, 2]
Eigenvalues[m]


is

{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}
0
{{30, 36, 42}, {66, 81, 96}, {102, 126, 150}}
{{30, 36, 42}, {66, 81, 96}, {102, 126, 150}}
{3/2 (5 + Sqrt[33]), 3/2 (5 - Sqrt[33]), 0}


where Transpose[m] computes the matrix transpose $$m^\intercal$$ (ConjugateTranspose[m] takes the transpose then converts each value to its complex conjugate, giving $$m^\dagger$$), Det[m] computes the determinant $$|m|$$, m . m computes the matrix product (generally, when $$A$$ has dimensions $$m\times{n}$$, $$B$$ has dimensions $$n\times{p}$$, this gives $$AB=A\times{B}$$ which has dimensions $$m\times{p}$$, such that $$(AB)_{i,j}=\sum_{k=1}^{n}A_{i,k}B_{k,j}$$), MatrixPower[m, n] computes $$m^n=m\times m\times m\times \ldots \times m$$, and EigenValues[m] gives the eigenvalues of the matrix $$m$$.

Eigenvalues

If you keep hearing the term “eigenvalues” or “eigenvectors” around the physics department but aren’t sure what they are, there’s a quick tutorial in the appendix about it. You might need to brush up on matrices a little beforehand. The tutorial can be found here.