Vectors and Matrices

Vectors and matrices in Mathematica are simply represented by lists and by lists of lists, respectively.

{a,b,c}vector
{{a,b},{c,d}}matrix

The representation of vectors and matrices by lists.

This is a 2×2 matrix.
In[1]:=
Click for copyable input
Out[1]=
Here is the first row.
In[2]:=
Click for copyable input
Out[2]=
Here is the element .
In[3]:=
Click for copyable input
Out[3]=
This is a two-component vector.
In[4]:=
Click for copyable input
Out[4]=
The objects and are treated as scalars.
In[5]:=
Click for copyable input
Out[5]=
Vectors are added component by component.
In[6]:=
Click for copyable input
Out[6]=
This gives the dot (scalar) product of two vectors.
In[7]:=
Click for copyable input
Out[7]=
You can also multiply a matrix by a vector.
In[8]:=
Click for copyable input
Out[8]=
Or a matrix by a matrix.
In[9]:=
Click for copyable input
Out[9]=
Or a vector by a matrix.
In[10]:=
Click for copyable input
Out[10]=
This combination makes a scalar.
In[11]:=
Click for copyable input
Out[11]=

Because of the way Mathematica uses lists to represent vectors and matrices, you never have to distinguish between "row" and "column" vectors.

Table[f,{i,n}]build a length-n vector by evaluating f with
Array[a,n]build a length-n vector of the form
Range[n]create the list
Range[n1,n2]create the list
Range[n1,n2,dn]create the list
list[[i]] or Part[list,i]give the i^(th) element in the vector list
Length[list]give the number of elements in list
c vmultiply a vector by a scalar
a.bdot product of two vectors
Cross[a,b]cross product of two vectors (also input as )
Norm[v]Euclidean norm of a vector

Functions for vectors.

Table[f,{i,m},{j,n}]build an m×n matrix by evaluating f with i ranging from 1 to m and j ranging from 1 to n
Array[a,{m,n}]build an m×n matrix with ^(th) element
IdentityMatrix[n]generate an n×n identity matrix
DiagonalMatrix[list]generate a square matrix with the elements in list on the main diagonal
list[[i]] or Part[list,i]give the i^(th) row in the matrix list
list[[All,j]] or Part[list,All,j]give the j^(th) column in the matrix list
list[[i,j]] or Part[list,i,j]give the ^(th) element in the matrix list
Dimensions[list]give the dimensions of a matrix represented by list

Functions for matrices.

Column[list]display the elements of list in a column
MatrixForm[list]display list in matrix form

Formatting constructs for vectors and matrices.

This builds a 3×3 matrix with elements .
In[12]:=
Click for copyable input
Out[12]=
This displays in standard two-dimensional matrix format.
In[13]:=
Click for copyable input
Out[13]//MatrixForm=
This gives a vector with symbolic elements. You can use this in deriving general formulas that are valid with any choice of vector components.
In[14]:=
Click for copyable input
Out[14]=
This gives a 3×2 matrix with symbolic elements. "Building Lists from Functions" discusses how you can produce other kinds of elements with Array.
In[15]:=
Click for copyable input
Out[15]=
Here are the dimensions of the matrix on the previous line.
In[16]:=
Click for copyable input
Out[16]=
This generates a 3×3 diagonal matrix.
In[17]:=
Click for copyable input
Out[17]=
c mmultiply a matrix by a scalar
a.bdot product of two matrices
Inverse[m]matrix inverse
MatrixPower[m,n]n^(th) power of a matrix
Det[m]determinant
Tr[m]trace
Transpose[m]transpose
Eigenvalues[m]eigenvalues
Eigenvectors[m]eigenvectors

Some mathematical operations on matrices.

Here is the 2×2 matrix of symbolic variables that was defined.
In[18]:=
Click for copyable input
Out[18]=
This gives its determinant.
In[19]:=
Click for copyable input
Out[19]=
Here is the transpose of .
In[20]:=
Click for copyable input
Out[20]=
This gives the inverse of in symbolic form.
In[21]:=
Click for copyable input
Out[21]=
Here is a 3×3 rational matrix.
In[22]:=
Click for copyable input
Out[22]=
This gives its inverse.
In[23]:=
Click for copyable input
Out[23]=
Taking the dot product of the inverse with the original matrix gives the identity matrix.
In[24]:=
Click for copyable input
Out[24]=
Here is a 3×3 matrix.
In[25]:=
Click for copyable input
Out[25]=
Eigenvalues gives the eigenvalues of the matrix.
In[26]:=
Click for copyable input
Out[26]=
This gives a numerical approximation to the matrix.
In[27]:=
Click for copyable input
Out[27]=
Here are numerical approximations to the eigenvalues.
In[28]:=
Click for copyable input
Out[28]=

"Linear Algebra in Mathematica" discusses many other matrix operations that are built into Mathematica.

New to Mathematica? Find your learning path »
Have a question? Ask support »