MATLAB - Sparse Matrix



A matrix is a two-dimensional array of numbers, symbols, or variables arranged in rows and columns. The dimensions of a matrix are given as "m x n," where "m" represents the number of rows, and "n" represents the number of columns.The individual elements of the matrix are usually referred to by their row and column indices, denoted as "a," where "i" indicates the row number and "j" indicates the column number.

Example

Here A is a 2x3 matrix i.e. it has 2 rows and 3 columns.

A =

   1     2     3
   4     5     6

Here A11 = 1 , A12 = 2 , A13 = 3. A21 = 4, A22 = 5 and A23 = 6.

What is a Sparse Matrix?

Sparse matrices are a type of matrix in which the majority of the elements are zero. So you will see a lot of zero elements in a sparse matrix in comparison to non-zero elements.

Why do we need Sparse Matrices?

Here are following benefits that we get from using sparse matrix −

Advantages of Sparse Matrices

  • Efficient Memory Usage − Sparse matrices are advantageous in terms of memory usage. Since most of the elements are zero, storing only the non-zero values can save a significant amount of memory compared to storing a dense matrix, which includes a large number of unnecessary zero values.
  • Computational Efficiency − Many operations involving sparse matrices can be optimized to take advantage of the sparsity structure. This leads to faster computations compared to dense matrices, where calculations involving a large number of zero values can be wasteful.
  • Reduced Complexity − Algorithms that operate on sparse matrices often exhibit lower complexity due to the reduced number of non-zero elements. This can result in faster algorithms and reduced processing time.
  • Solving Sparse Linear Systems − Sparse matrices frequently arise in systems of linear equations, and specialized techniques like iterative solver can be more efficient for solving sparse linear systems than direct methods used for dense matrices.

Disadvantages of Sparse Matrices

  • Complex Indexing − Operations involving sparse matrices often require more complex indexing and data structures to efficiently navigate and manipulate the non-zero elements. This complexity can make the implementation of algorithms involving sparse matrices more challenging.
  • Sparse Matrix-Vector Multiplication − While certain operations can be highly efficient with sparse matrices, matrix-vector multiplication can be less efficient compared to dense matrices, especially when the sparsity pattern is irregular.

Creation of Sparse Matrix

In MATLAB, you can create a sparse matrix using the sparse function. This function allows you to specify the non-zero values and their corresponding row and column indices. The sparse function automatically converts the non-zero elements of the dense matrix into a sparse format while omitting the zeros.

Syntax

S = sparse(A)

Here A is the dense matrix from which you want to create the sparse matrix.In a dense matrix, the majority or all of its elements are non-zero.

Let us create a sparse matrix from a dense matrix.

Example 1

A = [0, 0, 0, 0;0, 1, 0, 2; 0, 0, 3, 0;0, 4, 0, 5]
sparse_matrix = sparse(A)

In this example, A is the input dense matrix, and sparse_matrix is the resulting sparse matrix created using the sparse function. The function automatically extracts the non-zero values and their corresponding row and column indices from the dense matrix to create the sparse representation.

When you execute the same in Matlab the output is as follows −

>> A = [0, 0, 0, 0;0, 1, 0, 2; 0, 0, 3, 0;0, 4, 0, 5]
>> sparse_matrix = sparse(A)

A =

   0     0     0     0
   0     1     0     2
   0     0     3     0
   0     4     0     5

sparse_matrix =

Compressed Column Sparse (rows = 4, cols = 4, nnz = 5 [31%])

  (2, 2) -> 1
  (4, 2) -> 4
  (3, 3) -> 3
  (2, 4) -> 2
  (4, 4) -> 5
>>

Sparse Matrix of Nonzeros with Specified Size

Creating a sparse matrix of nonzeros with a specified size involves providing the dimensions of the matrix along with the non-zero elements' locations and values.

Example

num_rows = 5;
num_cols = 5;
nonzero_rows = [1, 2, 3, 4]
nonzero_cols = [2, 4, 1, 3]
nonzero_vals = [10, 20, 30, 40]
sparse_matrix = sparse(nonzero_rows, nonzero_cols, nonzero_vals, num_rows, num_cols)

In the example above, the sparse matrix created will have dimensions 5x5. It will have non-zero elements at positions (1,2), (2,4), (3,1), and (4,3), with corresponding values 10, 20, 30, and 40.

When you execute above example in matlab command window the output is −

>> num_rows = 5;
num_cols = 5;
nonzero_rows = [1, 2, 3, 4]
nonzero_cols = [2, 4, 1, 3]
nonzero_vals = [10, 20, 30, 40]
sparse_matrix = sparse(nonzero_rows, nonzero_cols, nonzero_vals, num_rows, num_cols)

nonzero_rows =

   1   2   3   4

nonzero_cols =

   2   4   1   3

nonzero_vals =

   10   20   30   40

sparse_matrix =

Compressed Column Sparse (rows = 5, cols = 5, nnz = 4 [16%])

  (3, 1) -> 30
  (1, 2) -> 10
  (4, 3) -> 40
  (2, 4) -> 20

>> 
Advertisements