Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Create sparse matrix

`S = sparse(A)`

`S = sparse(m,n)`

`S = sparse(i,j,v)`

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

`S = sparse(i,j,v,m,n,nz)`

`S = sparse(`

generates
a sparse matrix `i,j`

,`v`

)`S`

from the triplets `i`

, `j`

,
and `v`

such that `S(i(k),j(k)) = v(k)`

.
The `max(i)`

-by-`max(j)`

output
matrix has space allotted for `length(v)`

nonzero
elements. `sparse`

adds together elements in `v`

that
have duplicate subscripts in `i`

and `j`

.

If the inputs `i`

, `j`

, and `v`

are
vectors or matrices, they must have the same number of elements. Alternatively,
the argument `v`

and/or one of the arguments `i`

or `j`

can
be scalars.

MATLAB

^{®}stores sparse matrices in compressed sparse column format. For more information, see John R. Gilbert, Cleve Moler, and Robert Schreiber's Sparse Matrices In MATLAB: Design and Implementation.The

`accumarray`

function has similar accumulation behavior to that of`sparse`

.`accumarray`

groups data into bins using*n*-dimensional subscripts, but`sparse`

groups data into bins using 2-D subscripts.`accumarray`

adds elements that have identical subscripts into the output by default, but can optionally apply any function to the bins.`sparse`

only adds elements that have identical subscripts into the output.

`accumarray`

| `diag`

| `find`

| `full`

| `issparse`

| `nnz`

| `nonzeros`

| `nzmax`

| `spalloc`

| `speye`

| `spones`

| `sprandn`

| `sprandsym`

| `spy`

Was this topic helpful?