Back to Sparse Matrices

sparse

Create sparse matrix

Syntax

S = sparse(A)
S = sparse(m, n)
S = sparse(i, j, v)
S = sparse(i, j, v, m, n)

Description

Creates a sparse matrix. With one argument, converts a dense matrix to sparse CSR format. With two arguments (m,n), creates an empty m-by-n sparse matrix. With three arguments (i,j,v), creates a sparse matrix from row indices i, column indices j, and values v. With five arguments, additionally specifies the m-by-n dimensions.

Parameters

NameDescription
A/iDense matrix, or row index vector (1-based)
j/n(optional)Column index vector (1-based), or number of columns
v(optional)Values vector
m(optional)Number of rows
n(optional)Number of columns

Returns

Sparse matrix (CSR format)

Examples

Try It
>> S = sparse(eye(5))
Try It
>> S = sparse([1 2 3], [1 2 3], [10 20 30], 3, 3)
Try It
>> S = sparse(5, 5)  % empty 5x5 sparse

See Also