FUNCTION REFERENCE

sparse

Create a sparse array.

Create a sparse array.

Syntax

S = sparse(A)

Syntax

S = sparse(m, n)

Syntax

S = sparse(i, j, v)

Syntax

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

Syntax

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

sparse(A) stores the nonzeros of a full double or logical matrix; sparse(m,n) is an m-by-n all-zero sparse array; sparse(i,j,v,m,n) puts v(k) at row i(k), column j(k). A scalar v, or an i or j of length 1, is expanded to the common length. Repeated subscripts are summed in input order (logical v: combined with or), and sums of zero are not stored. Without m and n the size is max(i)-by-max(j). nzmax is checked and then ignored.

Example

S = sparse([1 2 3], [1 2 3], [4 5 6])   % 3x3 with three nonzeros

Example

full(sparse([1 1 2], [1 1 2], [2 3 4]))   % [5 0; 0 4]

Example

S = sparse(2)   % 1x1 sparse

Storage: compressed columns. Each column keeps its row indices in increasing order with their values (double, or logical for a logical sparse array). Zeros are never stored, including sums that cancel and rounding results that become 0. Only two dimensions. Assigning a sparse variable shares its storage; a write builds a new copy.

Results (scalar means 1-by-1, full or sparse): S+S and S-S are sparse; S+F, S-F, F-S and S+scalar are full. S.*S, S.*F, F.*S, S*scalar, scalar*S, S/scalar, S./scalar and scalar\S are sparse; multiplying or dividing by a scalar changes the stored values only. S*S is sparse; S*F and F*S are full. S\F and F/S with a square sparse S (not 1-by-1) solve without converting S: a triangular S by substitution, a symmetric S with a positive diagonal by sparse Cholesky (AMD ordering), another symmetric S by sparse LDL' (AMD ordering, Bunch-Kaufman pivoting within supernodes; LU is used instead when its relative residual exceeds 1e-10), otherwise sparse LU (COLAMD ordering, partial pivoting); the results are full, and S\S and S/S are sparse. A singular S, stored NaN or Inf, or a non-finite right-hand side uses the full solver when n*n fits the sparse-to-full limit (same values and warnings as full(S)\F) and is an error above it. A rectangular S, and S/X and X\S with a full non-scalar X, convert to full and use the full solver. S&S, S&F and F&S are logical sparse. -S, +S, S', S.', abs, round, floor, ceil and fix are sparse; sqrt is sparse when no stored value is negative. sum, max, min (one output), any and all are sparse; implicit zeros take part in max and min. double and logical keep sparse storage. [S, F], [F; S] and cat(1 or 2, ...) are sparse. find and nonzeros give full columns. Indexing a sparse array (S(i,j), S(k), S(:), masks, end) gives a sparse result, 1-by-1 included.

Converted to full first: every other function given a sparse input, including S./F, F./S, scalar./S, S.^p, p.^S, S|F, ~S, xor, the comparisons ==, ~=, <, <=, >, >=, [m,i] = max(S), max(S,F) and max(S,scalar). Their results are full.

Indexed writes: S(i,j) = v stores nonzeros and deletes entries set to 0; subscripts beyond the size grow the array (S(5,6) = 0 grows a 3-by-2 array to 5-by-6 and stores nothing). S(:,j) = [] and S(i,:) = [] delete columns and rows. Linear growth of a matrix beyond its number of elements is an error, as for full matrices. When an index repeats, the last value wins.

Bytes (whos and the workspace limit): 16 per stored double entry, 9 per stored logical entry, plus 8 per column pointer (number of columns + 1). This is MatRust's count, not MATLAB's.

Display: one line per stored entry in column order, (row,col) followed by the value as the current format shows a column of the stored values; an array without nonzeros shows All zero sparse: m-by-n.

MAT files: -v6 and -v7 save and load double and logical sparse arrays, also inside cell arrays.

Differences from MATLAB: The functions listed above as converted to full return full results where MATLAB keeps some of them sparse (S./F, S.^p, comparisons, ~S, S|F). A sparse array with more than 16777216 elements cannot be converted to full yet (the error names the function). Complex, single and integer sparse arrays are not supported; complex sparse variables in MAT files are reported and skipped while the other variables load. nzmax equals nnz. Sparse variables cannot be saved to v7.3 files yet. No sparse factorizations, eigs, svds, spfun or sprand yet.

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.