Sparse Function Syntax
Syntax:
s = sparse(matrix)
The above method converts an existing matrix into a sparse matrix. See the following example.
Example 1:
Matlab
% MATLAB code for 7 by 7 identity matrix identity = eye(7) % Converting identity into a sparse matrix s = sparse(identity) |
Output:
Sparse Matrices in MATLAB
Matrices are 2-dimensional arrays that are the most popular data type in MATLAB. A matrix can contain theoretically infinite elements as long as the computer has the required memory. In practice, when dealing with large data, there are scenarios when the majority of data contains zeroes as elements and this results in memory wastage.
As a resolution, sparse matrices are created. A sparse matrix is a representation of a matrix in which all zero elements are removed and non-zero elements are stored as row, column, and value triplet.
In this article, we shall see how to create a sparse matrix, convert an existing matrix to a sparse matrix, etc.