# sparse

## Purpose

Create sparse matrices.

## Synopsis

    S = sparse(A)
    S = sparse(i,j,s,m,n,nzmax)
    S = sparse(i,j,s,m,n)
    S = sparse(i,j,s)
    S = sparse(m,n)

## Description

`S = sparse(...)` is the built-in function, which generates matrices in MATLAB's sparse storage organization. It can be called with one, two, three, five, or six arguments.

`S = sparse(A)` converts a full matrix to sparse form by squeezing out any zero elements. If `S` is already sparse, `sparse(S)` returns `S`.

`S = sparse(i,j,s,m,n,nzmax)` uses vectors `i`, `j`, and `s` to generate an `m`-by-`n` sparse matrix with space allocated for `nzmax` nonzeros. The two vectors `i` and `j` contain the row and column coordinates, respectively, for the non-zero elements of the matrix. `s` contains the values for each non-zero element. Any elements of `s` that are zero are ignored, along with the corresponding values of `i` and `j`. `i`, `j`, and `s` are all the same length.

There are several simplifications of this six-argument call.

The argument `s` and one of the arguments `i` or `j` can be scalars, in which case they are expanded so that the first three arguments all have the same length.

`S = sparse(i,j,s,m,n)` uses `nzmax = length(s)`.

`S = sparse(i,j,s)` uses `m = max(i)` and `n = max(j)`. The maxima are computed before any zeros in `s` are removed, so one of the rows of `[i j s]` might be `[m n 0]`.

`S = sparse(m,n)` abbreviates `sparse([],[],[],m,n,0)`. This generates the ultimate sparse matrix, an `m`-by-`n` all zero matrix.

All of MATLAB's built-in arithmetic, logical, and indexing operations can be applied to sparse matrices, or to mixtures of sparse and full matrices. Operations on sparse matrices return sparse matrices and operations on full matrices return full matrices.

In most cases, operations on mixtures of sparse and full matrices return full matrices. The exceptions include situations where the result of a mixed operation is structurally sparse, for example, `A.`*`S` is at least as sparse as `S`.

Some operations, such as `S >= 0`, generate Big Sparse, or BS, matrices``- matrices with sparse storage organization but few zero elements.

## Examples

`S = sparse(1:n,1:n,1)` generates a sparse representation of the `n`-by-`n` identity matrix. The same `S` results from `S = sparse(eye(n,n))`, but this would also temporarily generate a full `n`-by-`n` matrix with most of its elements equal to zero.

`B = sparse(10000,10000,pi)` is probably not very useful, but is legal and works; it sets up a `10000`-by-`10000` matrix with only one nonzero element. Don't try `full(B);` it requires 800 megabytes of storage.

This dissects and then reassembles a sparse matrix:

    [i,j,s] = find(S);
    [m,n] = size(S);
    S = sparse(i,j,s,m,n);

So does this, if the last row and column have nonzero entries:

    [i,j,s] = find(S);
    S = sparse(i,j,s);

## See Also

    diag, find, full, nnz, nonzeros, nzmax, sparse, spones, sprandn,
    sprandsym, spy
    The sparfun directory

(c) Copyright 1994 by The MathWorks, Inc.