Updated May 31, 2023

## Introduction to Sparse Matrix in MATLAB

Sparse Matrix in MATLAB is meant for storing the data which has zeroes and non-zeroes value so it saves memory and helps in managing the data effectively. Suppose a matrix with x number of columns and y number of rows has less number of non-zero values as compared to the number of zeroes in that matrix, then it is known as Sparse matrix. Briefly, it is the matrix that contains less number of non-zero values. Sparse Matrix is used to reduce the scanning time and saves a lot of space.

There are 2 types of Sparse Matrix operations used in Matlab:

**Computational Complexity:**It is equal to the number of non-zero elements present in a sparse matrix. It is also dependent on its row and column size but it is independent on the product i.e. the number of elements present in a sparse matrix including both zero and non-zero elements.**Algorithmic Details:**There are various rules that sparse matrix should follow depending on the functions and operators used

### Creation and Working with Sparse Matrix in MATLAB?

Below are the creation and working with Sparse Matrix:

- Suppose we have 10*10 matrix which has only 5 non zero values and the rest other are zeroes. Generally, we require 10*10*2=40 bytes of memory to store this 2-dimensional matrix and to access the 5 non-zero elements present in a matrix we have to scan around 100 times which is not feasible and very time-consuming.
- So, to overcome this issue we can use sparse matrix representation. Generally, there are two types of representation used in the sparse matrix which are Triplet Representation and Linked Representation.
- In Matlab, we can create a sparse matrix by using the keyword “sparse”.

The syntax which is used to represent the sparse matrix in Matlab with additional features like:

`i = Sparse(M)`

- This is used to convert a normal matrix M to the sparse matrix which will squeeze out the zeroes present in the matrix and it helps in saving the memory. If a matrix contains many zeroes, then it does not automatically convert the matrix to the sparse matrix. We have to consider the density of the matrix to determine if we should convert the matrix to a sparse matrix or not.
- The density of a matrix is defined as the total number of non-zero elements present in the matrix divided by the total number of values present in the matrix. Generally, a matrix that has low density is considered a better fit to convert it into a sparse matrix.

Below are the examples of Sparse Matrix in MATLAB:

#### Example #1

To sparse a matrix if it has repeated values.

```
a = [5,5,5,6,9,9,10,10]
b= [2,2,2,1,3,3,9,9]
c= [200,300,100,400,500,600,700,800]
[a, b, c]
```

In the above example, if there are repeated elements for ath and bth value then the respective values for them are added and the resultant value is assigned. For example: in the case of 5 and 2 pairs, the values are added and the sum is assigned. If the matrix has such type of format, then it can be sparsed in the same way as shown in the above example.

The input matrix can be of the double and logical data types. It can also handle complex numbers. Sparse Matrix is meant to store only the elements that are not zero in a matrix which saves the memory and reduces the computational time. The space required by the elements does not depend on the type whether it is zero or non –zero. It will allocate the same amount of memory location to zero and non-zero elements.

If a matrix is large and it has many zeroes, then this reduces the memory required for storing the data. Sparse Matrices not only reduce the memory storage but also reduce the execution time. If we convert the normal into a sparse matrix, then unnecessary operations are not performed like low-level arithmetic calculations are not performed in the sparse matrix which further reduces the execution time of the calculation and provides the results. If it has huge data, then it is really helpful in executing any expression.

#### Example #2

To convert a full matrix to sparse matrix:

`M= ([2 5 4 1 3 ] [ 4 5 1 2 3 ] [1 2 3 4 5])`

In the above example, the first element is taken from both the list and combined with their indices. We can also import different sparse matrix from outside using different functions in Matlab.

There are various ways of processing and accessing the sparse matrix in Matlab like:

**nonzeros:**It returns all the non-zero elements present in a sparse matrix resulting in a column vector.**nnz:**It returns the count of the values that are not zero present in a sparse matrix.**nzmax:**It returns the memory size that is assigned to the elements which are not zero present in the sparse matrix.

Find function in Matlab is used to find the elements that are not zero, row and column indices of a sparse matrix. We can also insert or append an additional row in a matrix as shown in the below example:

#### Example #3

`C = 1 2 1`

3 2 2

4 5 2

5 2 1

C (3,1) = 52

[m, n, a] = find(C);

[m, n, a]

Here we are inserting a new row into the matrix and the other rows are shifted subsequently after adding the additional row. We can also visualize the non-zero elements of a sparse matrix which shows the distribution of non-zero elements present in the matrix. The function which is used to view the non-zero elements of the sparse matrix is the “spy” function, where each point present in the graph represents the location of each element which is not zero in a sparse matrix.

### Conclusion

Sparse Matrices are generally used to handle any large scale applications mainly dealing with any differential equations. We can also convert sparse matrix to full matrix using function “full” in Matlab which is not always desirable and we can check whether a matrix is sparse or full matrix using is sparse () function.

### Recommended Articles

This is a guide to Sparse Matrix in MATLAB. Here we discuss the 2 types of Sparse Matrix along with the creation and working with respective examples. You can also go through our other suggested articles to learn more–