Documentation
Mathematica
Built-in Functions
Advanced Documentation
Linear Algebra
Sparse Arrays

Structural Operations
Structural operations on sparse arrays are all equivalent to the operations on dense matrices.
Getting Pieces of Matrices
Extracting elements, rows, and columns of a sparse matrix is quite straightforward with the Mathematica function Part. Typically Part is entered with [[ ]] notation.

Ways to get pieces of matrices.
Here is a sample sparse matrix.
In[1]:=
Out[2]//MatrixForm=
This gets the third element in the first row.
In[3]:=
Out[3]=
This gets the third row; the row is returned as a sparse vector.
In[4]:=
Out[4]=
It can also obtain a column by using All to specify all rows; the column is returned as a sparse vector.
In[5]:=
Out[5]=
Negative indices are used to refer to the end of the matrix. The following gets the last element of the last row.
In[6]:=
Out[6]=
The function Tr works on the diagonal elements of a matrix. The one argument form adds them up.
In[7]:=
Out[7]=
Tr can also take a function as its second argument, which will apply to the diagonal elements. If List is used, this returns the diagonal elements.
In[8]:=
Out[8]=
Getting Multiple Pieces
It is possible to extract multiple elements by using indices in lists. This will be demonstrated with the following sample matrix.
In[1]:=
Out[2]//MatrixForm=
The following gets the first and third elements of the third row.
In[3]:=
Out[3]=
The following gets the first and third rows.
In[4]:=
Out[4]=
The following gets the first and third elements of the first and second rows.
In[5]:=
Out[5]=
Setting Pieces of Matrices
Setting elements, rows, and columns so that a sparse matrix is updated is quite straightforward by using the Mathematica function Part on the left-hand side of an assignment.

Resetting parts of matrices.
Here is a sample sparse matrix.
In[1]:=
Out[2]//MatrixForm=
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.
In[3]:=
Out[4]//MatrixForm=
This sets the second row of the matrix.
In[5]:=
Out[6]//MatrixForm=
Here, the second column is set.
In[7]:=
Out[8]//MatrixForm=
Setting Multiple Pieces
It is possible to set multiple elements by using indices in lists. This will be demonstrated with the following sample matrix.
In[1]:=
Out[2]//MatrixForm=
The following sets the first and third elements of the second row.
In[3]:=
Out[4]//MatrixForm=
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.
In[5]:=
Out[6]//MatrixForm=
The following sets the second and third rows.
In[7]:=
Out[8]//MatrixForm=
The following gives two different values for the second and third rows.
In[9]:=
Out[10]//MatrixForm=
The following sets the first and third elements of the second and third rows.
In[11]:=
Out[13]//MatrixForm=
The following sets the first and third elements of the second and third rows with different values.
In[14]:=
Out[15]//MatrixForm=
Extracting Submatrices
Take is useful to extract a submatrix.

Extracting submatrices.
In[1]:=
Out[2]//MatrixForm=
This extracts the submatrix from
to
.
In[3]:=
Out[3]//MatrixForm=
This extracts the submatrix of rows 1 to 3.
In[4]:=
Out[4]//MatrixForm=
This extracts the submatrix of columns 2 to 4.
In[5]:=
Out[5]//MatrixForm=
You can use negative indices to count from the end.
This returns the matrix with the first and last columns dropped.
In[6]:=
Out[6]//MatrixForm=
Deleting Rows and Columns
If you want to delete rows or columns, you can use Drop.

Deleting rows and columns.
In[1]:=
Out[2]//MatrixForm=
This drops rows 2 through 4.
In[3]:=
Out[3]//MatrixForm=
This drops columns 2 through 4.
In[4]:=
Out[4]//MatrixForm=
In this example rows 2 and 3, and columns 1, 2, and 3 are all dropped.
In[5]:=
Out[5]//MatrixForm=
Inserting Rows and Columns
If you want to insert a row, you can use Insert.

Inserting a row.
In[1]:=
Out[2]//MatrixForm=
This inserts a row before row 3.
In[3]:=
Out[3]//MatrixForm=
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.
In[4]:=
Out[4]//MatrixForm=
Extending Matrices
You can increase the size of a matrix by padding it with PadLeft and PadRight.
In[1]:=
Out[2]//MatrixForm=
PadLeft adds the elements to the beginning of the matrix. This example returns a 4
4 sparse matrix.
In[3]:=
Out[3]=
You can see the result with MatrixForm.
In[4]:=
Out[4]//MatrixForm=
PadRight adds the elements to the end of the matrix; the result is a sparse array.
In[5]:=
Out[5]//MatrixForm=
You can use PadLeft to pad on the right if you specify negative indices.
In[6]:=
Out[6]//MatrixForm=
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.
In[7]:=
Out[8]//MatrixForm=
In[9]:=
Out[9]//MatrixForm=
PadLeft and PadRight have a number of extra features. These are described in the Mathematica documentation.
Transpose
Transposition of elements is a general matrix operation.
In[1]:=
Out[2]//MatrixForm=
Transpose swaps elements at specific indices; the result is a sparse array.
In[3]:=
Out[3]=
In[4]:=
Out[4]//MatrixForm=
If you wish to compute the conjugate transpose of a sparse matrix, you can combine the functions Transpose and Conjugate.
In[5]:=
Out[7]//MatrixForm=
If a matrix is equal to its conjugate transpose, it is said to be hermitian.
In[8]:=
Out[8]=
Rotating Elements
Another structural operation is to rotate elements within an index. This can be done with the functions RotateLeft and RotateRight.
In[1]:=
Out[2]//MatrixForm=
This rotates left by one step in the first level, thereby operating on the rows; the result is a sparse array.
In[3]:=
Out[3]=
In[4]:=
Out[4]//MatrixForm=
This rotates left by one step in the second level, that operates on the columns; the result is a sparse array.
In[5]:=
Out[5]//MatrixForm=
This rotates the columns in the opposite direction.
In[6]:=
Out[6]//MatrixForm=
Testing Matrices
Mathematica provides a number of functions for testing sparse matrices and extracting size information.

The predicate MatrixQ can be used to test sparse matrices. It can also be used to test dense matrices as shown previously.
In[1]:=
Out[2]//MatrixForm=
This tests that sp is a sparse matrix.
In[3]:=
Out[3]=
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[4]:=
Out[4]=
In the example below, every element must be an integer greater than 1. Because some of the elements are 0, the result is False.
In[5]:=
Out[5]=
In addition to MatrixQ, there are predicates VectorQ and ArrayQ, which are useful for testing vectors and tensors.
In[6]:=
Out[6]=
In[7]:=
Out[7]=
ArrayQ can also take a rank argument to test the depth of the array. In this example, the sparse argument is not a rank-4 tensor and the result is False.
In[8]:=
Out[8]=
ArrayQ also takes a third argument that tests each element. In this example the result is True because all the elements are NumberQ.
In[9]:=
Out[9]=
The command Dimension is useful for extracting size information.
In[10]:=
Out[11]//MatrixForm=
In[12]:=
Out[12]=
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; this is equivalent to Length[Dimensions[sp]].
In[13]:=
Out[13]=
To compare if the elements of two matrices are equal it is possible to use Equal, typically entered using a == short-hand notation. For example, comparing a matrix with itself returns True.
In[14]:=
Out[15]=
Equal uses the value of numbers so it can be used to compare integer and real values.
In[16]:=
Out[16]=
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.

This generates a sample matrix for demonstration.
In[1]:=
Out[2]//MatrixForm=
This flattens the matrix into a vector.
In[3]:=
Out[4]=
The vector can be partitioned back into a matrix with rows of length 3 using Partition.
In[5]:=
Out[5]=
Matrices can be joined together with the function Join.
In[6]:=
Out[6]//MatrixForm=
A new row can be inserted at the end of a matrix with Append.
In[7]:=
Out[7]//MatrixForm=
It should be noted that this can also be done with Insert. This is shown in the section Inserting Rows and Columns.