Additional functionality related to this tutorial has been introduced in subsequent versions of the Wolfram Language. For the latest information, see Matrices and Linear Algebra.

Matrix and Tensor Operations

This tutorial reviews the functions that Mathematica provides for building and working with matrices, vectors, and tensors. It focuses on functions that are specific to Mathematica, and uses matrices for many of the examples. However, all the functions are general, and they will also work for vectors and tensors.

Building Matrices

Matrices are represented in Mathematica with lists. They can be entered directly with the { } notation that Mathematica provides for lists. An example of a matrix is shown here; by default a matrix prints with the list notation.

Mathematica provides a number of ways to build matrices.

Table[f,{i,m},{j,n}]build an m×n matrix where f is a function of i and j that gives the value of i,j th entry
Array[f,{m,n}]build an m×n matrix whose i,j th entry is f[i,j]
DiagonalMatrix[list]build a diagonal matrix with the elements of list on the diagonal
IdentityMatrix[n]build an n×n identity matrix
ConstantArray[val,{m,n}]]build an m×n matrix with each element being val
RandomReal[{0,val},{m,n}]]build an m×n matrix with random entries
Normal[SparseArray[{{i1,j1}->v1,{i2,j2}->v2,},{m,n}]]
build an m×n matrix with nonzero values νk at positions {ik,jk}
The function Table is particularly important to generate matrices, when there is not a built-in function to generate your matrix. The Mathematica documentation contains more information on Table. In this example a 3×3 matrix is built:
MatrixForm prints the matrix in a two-dimensional form:
Another way to build matrices in Mathematica is with the Array command. Here it is used to build a 3×3 matrix:
Mathematica has a number of functions for building certain specialized matrices. This builds the identity matrix:
Note that the identity matrix that is generated is an integer matrix. If you want to compute with floating-point numbers it can be advantageous to use matrices that contain floating-point entries; this is described in more detail under "Matrix Contents". If you start with an integer matrix, such as the identity matrix generated previously, it can be converted to a floating-point matrix by using the function N. This is shown in the following:
Here a diagonal matrix is constructed:
In this example, the elements are put on the first diagonal. MatrixForm is useful for showing the results:
This makes a 5×5 matrix; each element is 2:
Here a 2×4 matrix with pseudorandom real numbers in the range 0 to 5 is built:
This makes a 3×4 matrix with pseudorandom integers in the range -5 to 5:
This makes a 3×4 matrix with two nonzero values filled in. SparseArray expressions are described under "Working with Sparse Arrays":
SparseArray[{},{n,n}]a zero matrix
SparseArray[{i_,i_}->1,{n,n}]an n×n identity matrix
SparseArray[{i_,j_}/;i>=j->1,{n,n}]
a lower-triangular matrix

Constructing special types of matrices with SparseArray.

This sets up a general lower-triangular matrix:

Functions that can read in matrices from files are discussed in the section "Import and Export" of matrices.

Special Matrices

Mathematica contains definitions for a number of special matrices.

HilbertMatrix[n]create an n×n Hilbert matrix, with elements given by 1/(i+j-1)
HilbertMatrix[{m,n}]create an m×n Hilbert matrix
HankelMatrix[n]create an n×n Hankel matrix with the first column given by 1, 2, , n and zeros beneath the main antidiagonal
HankelMatrix[list]create a Hankel matrix with the first column given by list and zeros beneath the main antidiagonal
HankelMatrix[col,row]create a Hankel matrix with the first column given by the list col and the last row given by the list row

Special matrices.

Here is a 2×4 Hilbert matrix:
The elements of the Hankel matrix can be given as a list:
The Hankel matrix can be filled with nonzero values by giving the final row. This also allows a nonsquare matrix to be generated. Note that the last element of the first column and the first element of the final row must be identical:

Structural Operations

These operations are all related to the structure of matrices. Many of the techniques shown in this section can be applied to Mathematica expressions and are not specific just to matrices.

Getting Pieces of Matrices

Extracting elements, rows, or columns of a matrix is quite straightforward with the Mathematica function Part. Typically Part is entered with [[ ]] notation.

m[[i,j]]the i,j th entry
m[[i]]the i th row
m[[i;;i]]rows i through j
m[[All,i]]the i th column
m[[All,i;;j]]columns i through j
m[[{i1,,ir},{j1,,js}]]the r×s submatrix with elements having row indices ik and column indices jk
Tr[m,List]list of the diagonal elements of m

Ways to get pieces of matrices.

Define the following matrix.
This gets the second element in the first row:
This gets the third row:
It can also obtain a column by using All to specify all rows:
Negative indices are used to refer to the end of the matrix. The following gets the last element of the last row:
You can get ranges of a matrix using ;;. This gets the second through the fourth rows:
This gets the second through the fourth columns:
You can also give a step. This gets every other column:
The function Tr works on the diagonal elements of a matrix. The one-argument form adds them up:
Tr can also take a function, as its second argument, to apply to the diagonal elements. If List is used this returns the diagonal elements:

It should be noted that these commands for extracting parts of matrices will work for any Mathematica expression.

Getting Multiple Pieces

It is possible to extract multiple elements by using indices in lists. This is demonstrated with the following sample matrix:
The following gets the first and third elements of the second row:
The following gets the second and fourth rows:
The following gets the first and third elements of the second and fourth rows:

Setting Pieces of Matrices

Setting elements, rows, and columns so that a matrix is updated is quite straightforward using the Mathematica function Part on the left-hand side of an assignment.

m={{a11,a12,},{a21,a22,},}assign m to be a matrix
m[[i,j]]=vreset element {i,j} to be v
m[[i]]=vreset all elements in row i to be v
m[[i]]={v1,v2,}reset elements in row i to be {v1,v2,}
m[[All,j]]=vreset all elements in column j to be v
m[[All,j]]={v1,v2,}reset elements in column j to be {v1,v2,}

Resetting parts of matrices.

Here is a 5×5 matrix:

To update parts of a matrix you can use Part on the left-hand side of an assignment.

This sets the third element of the third row:
This sets the second row to have a particular value:
This sets the second column:
It is also possible to use negative indices to count from the end of the matrix. The following example sets the last element of the last row:
You can also use the range syntax for setting pieces of the matrix. This sets every element in every other row to be z:

Setting Multiple Pieces

It is possible to set multiple elements by using indices in lists. This is demonstrated with the following sample matrix:
The following sets the first and third elements of the second row:
If the right-hand side of the assignment is a list that matches the number of elements being assigned, the assignment is done element by element. Thus, the following gives two different values for the first and third elements of the second row:
The following sets the second and fourth rows:
The following gives two different values for the second and fourth rows:
The following sets the first and third elements of the second and fourth rows:
The following sets the first and third elements of the second and fourth rows with different values:

Extracting Submatrices

The range syntax is useful to extract a submatrix.

m[[i0;;i1,j0;;j1]]extract the submatrix with rows i0 through i1 and columns j0 through j1
m[[i0;;i1]]extract the submatrix with rows i0 through i1
m[[All,j0;;j1]]extract the submatrix with columns j0 through j1

Extracting submatrices.

This extracts the submatrix from m2,1 to m3,2:
This extracts the submatrix of rows 1 to 3:
This extracts the submatrix of columns 2 to 4:

You can use negative indices to count from the end.

This returns the matrix with the first and last columns dropped:

Deleting Rows and Columns

If you want to delete rows or columns you can use Drop.

Drop[m,{i0,i1}]delete rows i0 through i1
Drop[m,{},{j0,j1}]delete columns j0 through j1
Drop[m,{i0,i1},{j0,j1}]delete rows i0 through i1 and columns j0 through j1

Deleting rows and columns.

This drops rows 2 through 4:
This drops columns 2 through 4:
In this example rows 2 and 3, and columns 1, 2, and 3 are all dropped:

Inserting Rows and Columns

If you want to insert a row, you can use Insert.

Insert[m,r,i]insert row r into matrix m at position i

Inserting a row.

This inserts a row before row 3:
If you want to insert a column, you must transpose the matrix, insert the column as a row, and then transpose the matrix back. This inserts a column before column 5:

Extending Matrices

You can increase the size of a matrix by padding it with PadLeft and PadRight.
PadLeft adds the elements to the beginning of the matrix:
PadRight adds the elements to the end of the matrix:
One important use of the padding functions is to replicate and tile a matrix. In this example the input matrix is extended to have two versions in every row and three in every column:
PadLeft and PadRight have a number of extra features and are completely general so that they work for arbitrary rank tensors. These are described in the Mathematica documentation.

Transpose

Transposition of elements is a general matrix operation:
Transpose swaps elements at specific indices:
If you wish to compute the conjugate transpose of a matrix, use the function ConjugateTranspose:
If a matrix is equal to its conjugate transpose, it is said to be Hermitian:
You can also test with the function HermitianMatrixQ:

Rotating Elements

Another structural operation is to rotate elements within an index. This can be done with the functions RotateLeft and RotateRight:
This rotates left by one step in the first level, which operates on the rows:
This rotates left by one step in the second level, which operates on the columns:
This rotates the columns in the opposite direction:

Testing Matrices

Mathematica provides a number of functions for testing matrices and extracting size information.

MatrixQ[expr]give True if expr has the form of a matrix, and False otherwise
Dimensions[expr]a list of the dimensions of a vector or matrix
mi==mjcompare elements of two matrices for equality

Functions for testing the structure of vectors, matrices, and arrays.

If you want to test that an expression is a matrix, you can use the predicate MatrixQ:
The integer 5 is not a matrix:
A matrix has to have the same lengths in every dimension:
MatrixQ takes an optional second argument that specifies a test to apply to every element. In this example every element is tested to see if it is an integer:
In this example every element must be an integer greater than 1:
The command Dimensions is useful for extracting size information:
To compare if the elements of two matrices are equal, it is possible to use Equal, typically entered using a == shortcut notation. For example, comparing a matrix with itself returns True:
Equal uses the value of numbers so it can be used to compare integer and real values:

It should be noted that Equal works on any Mathematica expression. If you want to compare two matrices for equality using properties of the matrix as a whole, it may be better to compare matrix norms. These are discussed in a later section.

Further Structural Operations

This section discusses some further structural operations that are useful for working with matrices.

Flatten[m]flatten out nested lists in m
Flatten[m,n]flatten out nested lists in m to level n
Partition[m,n]partition m into sublists of length n
Join[m1,m2]concatenate m1 and m2
Append[m,r]insert row r at the end of m
Prepend[m,r]insert row r at the beginning of m
This generates a sample matrix for demonstration:
This flattens the matrix into a vector:
Here the vector is partitioned back into a matrix with rows of length 4 using Partition:
Matrices can be joined together with the function Join. This inserts the new matrix as new rows:
Alternatively, you can join the new matrix as new columns:
A new row can be inserted at the end of a matrix with Append:

It should be noted that this can also be done with Insert; see the section "Inserting Rows and Columns".

Element-wise Operations

If you want to operate on the elements of a matrix, you can do this easily with Mathematica. First, build a matrix of floating-point numbers:
Arithmetic operations applied to a matrix thread down to work on each element. Thus, if 5 is added to the matrix, the result is equivalent to adding 5 to each element:
Here, every element of the matrix is doubled:
Here, every element of the matrix is squared:
If one matrix is divided by another, the division is done element by element. If the dimensions of the two matrices do not agree, there is an error:
To apply the Sin function to every element, you apply Sin to the entire matrix:
If both of the arguments of an operation are matrices, the operation is carried out on corresponding elements:
Note that the dimensions of the two matrices have to be compatible:
If one of the arguments is a matrix and the other is a vector, the operation is carried out between rows of the matrix and elements of the vector:
Note that the multiplication of two matrices using the operator Times (typically this is entered by placing two arguments together) produces a matrix with elements that are products of the corresponding elements of the matrices. If you want to carry out matrix multiplication, this can be done with the function Dot and is described in the section "Matrix Multiplication". An example of element-wise multiplication is shown in the following example:

Listability

If you want to apply your own function to each element in a matrix, you may do this by giving your function the attribute Listable. This function squares each element and divides the result by 3:
Of course, the function will still work with symbolic matrices:

Map

Instead of using listability you can use Map to apply a function to every element in a matrix.
This says that the function f should be applied to every element in the matrix:
Here, a function that squares its argument and divides the result by 3 is applied to every element in the matrix:

Vectors and Tensors

In addition to supporting matrices, Mathematica supports vectors and tensors. All of these are built from lists. As described in "Introduction to Linear Algebra in Mathematica", Mathematica uses the term tensor to refer to generalized matrices. All the operations for building matrices can be generalized to work for vectors and tensors. Mathematica vectors have one level of list.

Here a vector is constructed:

In addition, there are a number of functions that generate vectors.

Table[f,{i,n}]build a lengthn vector by evaluating f with i=1,2, ,n
Array[a,n]build a lengthn vector of the form {a[1],a[2],}
Range[n]create the list {1,2,3, ,n}
Range[n1,n2]create the list {n1,n1+1,n2}
Range[n1,n2,dn]create the list {n1,n1+dn ,n2}

Functions for generating vectors.

One very efficient way to generate a vector is to use the Mathematica function Range. This generates a vector of integers:
This generates a vector of reals, starting with 1., incrementing in 0.1, and ending at 4:
Table can also be used to programmatically construct a vector; this can be useful if you want to execute a function to determine the elements of the vector:
Though it is often more efficient to use listable operations:
The operations and functions discussed in previous sections have equivalent versions for vectors:

It should be noted that Mathematica has no concept of a row or a column vector; a vector has a single index that can be used to reference an element of the vector.

Tensors can also be built using the Table command. Here, three iterators are used to generate a 2×3×4 tensor:
You can extract an element by using Part with three indices:

Testing Vectors and Tensors

Mathematica provides a number of functions for testing vectors and tensors and extracting size information.

VectorQ[expr]give True if expr has the form of a vector, and False otherwise
MatrixQ[expr]give True if expr has the form of a matrix, and False otherwise
ArrayQ[t,n]test whether t is a tensor of rank n
Dimensions[expr]a list of the dimensions of a vector or matrix
ArrayDepth[t]find the rank of a tensor
ti==tjcompare elements of two tensors for equality

Functions for testing the structure of vectors, matrices, and arrays.

If you want to test that an expression is a matrix you can use the predicate MatrixQ:
In addition to MatrixQ, VectorQ and ArrayQ are useful for testing vectors and tensors:
ArrayQ can also take a rank argument to test the depth of the array:
ArrayQ also takes a third argument that tests each element. In this example the result is False because not all the elements are NumberQ:
The command Dimensions is useful for extracting size information:
Dimensions returns a list of length 2 when the input is a matrix, stating that two indices are used to reference any element in the matrix. Another way to test the number of indices required to reference elements is with ArrayDepth. In this example the result is 2:
To compare if the elements of two tensors are equal it is possible to use Equal, typically entered using a == shortcut notation. For example, comparing a tensor with itself returns True:
Equal uses the value of numbers, so it can be used to compare integer and real values:
If the structure of two objects does not match they are not equal:

Visualization of Matrices

This section reviews the functions that are available for formatting and plotting matrices.

MatrixForm[mat]print a matrix with the elements arranged in a twodimensional array
MatrixPlot[mat]show the structural pattern of mat

Formatting Matrices

Matrices can be formatted with the function MatrixForm:
MatrixForm also works for vectors and higher-rank tensors; the braces can help you to understand the grouping:

Plotting Matrices

A convenient way to plot matrices is with the function MatrixPlot:

MatrixPlot has a number of graphics options for controlling the appearance of the plot. Many of these are the typical options of Mathematica DensityGraphics objects. It also has a special option, MaxPlotPoints, that controls the maximum size for the display of a matrix. Above this size the matrix is downsampled. Some of the important options are summarized in the following.

option name
default value
MaxPlotPoints200maximum size of matrix to display
AspectRatio1scaling for the final image shape
ColorRulesAutomaticcoloring for each element
ColorFunctionAutomaticfunction for coloring each element
MeshFalsewhether to draw a mesh
MeshStyleGrayLevel[1]the style of the mesh

Options of MatrixPlot.

One useful feature of MatrixPlot is that it can plot very large matrices. Here a very large sparse matrix is generated:
You can still plot this out with MatrixPlot. Here the number of points that are plotted is reduced to 100:
By default, no mesh is drawn around the elements; this can be enabled with the Mesh option. Typically you would not want to draw the mesh if the matrix is very large. When the mesh is drawn, it is sometimes useful to change the style of the mesh with the MeshStyle option:
You can change the coloring with the ColorRules option. This example plots zero as white and everything else black:
You can also use the ColorFunction option to add color:

Import and Export of Matrices

This section reviews the functions that are available for importing and exporting matrices.

Import[file,format]import data in the specified format from a file
Export[file,mat,format]export matrix to a file, converting it to the specified format

Mathematica provides a number of different tools for input/output. If you want to save your data in a file so that later you or a colleague can continue to work with it in Mathematica, you might want to use some of the functions that work with Mathematica expressions in files. These are discussed under "Expression Input and Output".

If you want to work with matrices from a source external to Mathematica using specific data formats, the functions Import and Export are useful. The Import function supports a variety of different formats, some of which are relevant to matrices:
The Table format will read tabular data into Mathematica. This format is not specific to matrices and can process different types of information such as numbers, dates, and currencies. However, this is often a simple way to read in a matrix. This example displays a sample data file:
Now the data file is read into Mathematica with the Import command using a format of Table; the result is a matrix:
Export can be used to write out a matrix in some particular format. Here it uses the Table format:
This example shows how to write a matrix into a CSV format. This could be read into another application such as a spreadsheet:

There are other matrix formats. For example, HarwellBoeing, used for sparse matrices, and Matrix Market, used for both sparse and dense matrices. These are discussed under "Import and Export of Sparse Matrices". In addition the MAT matrix format and the FITS astronomical data format can also be useful as import or export formats.

Matrix Multiplication

Matrix multiplication (also called dot or inner product) is carried out in Mathematica with the function Dot, typically entered with a dot shorthand syntax:
This demonstrates matrix multiplication of a matrix with itself:
This multiplies a matrix with a vector:
The matrix product can be computed with two matrices of different sizes so long as they are compatible. For matrices this means that to multiply a m1×n1 matrix by a m2×n2 matrix, it is required that n1 is equal to m2. Here, a 2×3 matrix is multiplied by a 3×2 matrix.
This will result in a 2×2 matrix:
This generates a 3×3 matrix:
If the dimensions do not match, an error is generated:
Dot can be used to multiply vectors of equal length; the result will be a scalar:
Multiplication of a matrix by a vector works equivalently. This multiples a 2×3 matrix by a length 3 vector:
This multiplies the length 3 vector by a 3×2 matrix:

The definition of matrix multiplication is such that the product of two matrices and , where , is given as follows.

The definition generalizes, so that the product of two arbitrary rank tensors and is as follows.

Thus applying Dot to a rank tensor and a rank tensor results in a rank tensor. An example is shown next.

First, a 2×3×4 tensor is defined:
Now, a 4×2×1 tensor is defined:
This multiplies tensor1 by tensor2. They are compatible because the length of the innermost index of tensor1 equals the length of the outermost index of tensor2:
The result is a 2×3×2×1 tensor:

Outer Product

The outer product is a way to build a higher-rank tensor from those of lower rank. Mathematica provides this functionality with the function Outer. One use of this is to combine two vectors to form a matrix as an outer product:
The function that is used to combine corresponding elements is given as the first argument. It can be an unknown function as in the following example:

Visualization of the Outer Product

One way to visualize the operation of Outer is demonstrated in this example. First, a list of points is created:
This shows how Outer joins each point to each other point:

Generalized Inner Product

Matrix multiplication is a fundamental operation of linear algebra computation. Consequently, Mathematica provides Dot as a dedicated function, which is heavily optimized. However, a generalization of matrix multiplication is provided by Inner. This allows the two operations that are used to form the product to be specified.

Here are two vectors:
This is the scalar product:
This is the equivalent operation using Inner:
Now Power is used instead of Times:

Matrix Permutations

Many matrix techniques rely on ordering a matrix in particular ways. For example, some techniques try to order the matrix to put elements on the diagonal, while others try to group certain elements into dense blocks. The Mathematica function Part is very well suited to applying permutations to the rows and columns of a matrix.

m[[perm]]apply a permutation to the rows of a matrix
m[[All,perm]]apply a permutation to the columns of a matrix
m[[perm,perm]]apply a permutation to the rows and columns of a matrix
m[[perm]]=mapply the inverse of a permutation to the rows of a matrix
m[[All,perm]]=mapply the inverse of a permutation to the columns of a matrix

Applying permutations to matrices.

This generates a random matrix:

Now the matrix will be reordered so that the rows are ordered by increasing size of 2-norm. (Norms are discussed under "Matrix Computations: Norms".)

First, the norm of each row is computed:
This computes the permutation that puts the smallest numbers first:
This applies the ordering to the rows of the matrix; the result has rows ordered by increasing size of 2-norm:
Now the inverse permutation is applied by using part assignment. Note that this modifies the matrix held by the symbol pmat. Part assignment is described previously:
If the inverse permutation is applied to the permuted matrix, the original matrix is restored:

Permutation Matrices

Another way to work with permutations is to compute a matrix that applies the permutation by multiplication. For example, this builds a 4×4 matrix:
This computes the permutation necessary to order the rows according to ascending 2-norm:
The permutation matrix is typically a sparse matrix, which is discussed under "Working with Sparse Arrays". This input generates a sparse identity matrix:
If the permutation is applied to the identity matrix, a permutation matrix is generated:
This applies the permutation. Note that the rows with smallest norms are at the top:
This applies the inverse permutation:

Typically it is faster to use the Mathematica function Part to apply a permutation, but sometimes it is convenient to work with a permutation matrix.