Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.1.1 - English

Change language to:
Français - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Sparse Matrix > Sparse Matrix Conversion > sparse

sparse

sparse matrix definition

Syntax

sp = sparse(X)
sp = sparse(ij, v)
sp = sparse(ij, v, mn)

Arguments

X

real or complex or boolean full (or sparse) matrix

ij

two columns integer matrix (indices of non-zeros entries)

v

vector

mn

integer vector with two entries (row-dimension, column-dimension)

sp

sparse matrix

Description

sparse is used to build a sparse matrix. Only non-zero entries are stored.

sp = sparse(X) converts a full matrix to sparse form by squeezing out any zero elements. (If X is already sparse sp is X).

sp=sparse(ij,v [,mn]) builds an mn(1)-by-mn(2) sparse matrix with sp(ij(k,1),ij(k,2))=v(k). ij and v must have the same column dimension. If optional mn parameter is not given the sp matrix dimensions are the max value of ij(:,1) and ij(:,2) respectively.

  • For the building syntaxes, if several values are given for a same ij position, the sum of theses values is assigned.
  • sparse([],[]) yields the (0, 0) zero sparse matrix, with sparse([],[]) ~= [].
Operating sparse matrices

Operations (concatenation, addition, etc,) with sparse matrices are made using the same syntax as for full matrices.

Elementary functions are also available (abs, min, max, sum, diag,...) for sparse matrices.

Mixed operations (full-sparse) are allowed. Results are full or sparse depending on the operations.

Any operation involving dense matrices of the same size, either as argument (e.g. sp=sparse(d)) or as result (e.g. d= sp + 1.) is provided for convenience purposes but should of course be avoided.

Furthermore, random access to elements (sp(r,c)), especially for insertions, is not efficient. So, any performance-constrained access should be done in batches with spget for read access, and with any sp=sparse(ij, v, mn) call for write access.

Examples

sp = sparse([1,2;4,5;3,10],[1,2,3])
size(sp)

x = rand(2,2);
abs(x) - full(abs(sparse(x)))

// sparse constructor taking a single dense matrix
// removes the zeros.
dense = [0., 1., 0., 0., 0.,
1., 0., 2., 0., 0.
0., 0., 0., 0., 0.
0., 0., 0., 0., -0.5];
sp = sparse(dense)

// complex matrices are also supported
sp = sparse(dense*(1+2*%i))

// for boolean matrices, the boolean sparse matrix
// only stores true values (and removes false values).
dense = [%F, %F, %T, %F, %F
%T, %F, %F, %F, %F
%F, %F, %F, %F, %F
%F, %F, %F, %F, %T];
sp = sparse(dense)

See also

  • full — sparse to full matrix conversion
  • spget — retrieves entries of sparse matrix
  • sprand — sparse random matrix
  • speye — sparse identity matrix
  • diag — diagonal including or extracting
  • toeplitz — Toeplitz matrix (chosen constant diagonal bands)
  • sparse(-0) — Processing of -0 versus 0

History

VersionDescription
6.0.2 sparse([],[]) now yields the "(0,0) zero sparse matrix" instead of [].
Report an issue
<< sp2adj Sparse Matrix Conversion spcompack >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jan 03 14:23:27 CET 2022