Mathematics |
Viewing Sparse Matrices
MATLAB provides a number of functions that let you get quantitative or graphical information about sparse matrices.
This section provides information about:
Information About Nonzero Elements
There are several commands that provide high-level information about the nonzero elements of a sparse matrix:
nnz
returns the number of nonzero elements in a sparse matrix.
nonzeros
returns a
column vector containing all the nonzero elements of a sparse matrix.
nzmax
returns
the amount of storage space allocated for the nonzero entries of a sparse matrix.
To try some of these, load the supplied sparse matrix west0479
, one of the Harwell-Boeing collection.
This matrix models an eight-stage chemical distillation column.
nnz(west0479) ans = 1887 format short e west0479 west0479 = (25,1) 1.0000e+00 (31,1) -3.7648e-02 (87,1) -3.4424e-01 (26,2) 1.0000e+00 (31,2) -2.4523e-02 (88,2) -3.7371e-01 (27,3) 1.0000e+00 (31,3) -3.6613e-02 (89,3) -8.3694e-01 (28,4) 1.3000e+02 . . . ans = 1.0000e+00 -3.7648e-02 -3.4424e-01 1.0000e+00 -2.4523e-02 -3.7371e-01 1.0000e+00 -3.6613e-02 -8.3694e-01 1.3000e+02 . . .
Note that initially nnz
has the same value as nzmax
by default. That is, the number of nonzero elements is equivalent to the number of storage locations allocated for nonzeros. However, MATLAB does not dynamically release memory if you zero out additional array elements. Changing the value of some matrix elements to zero changes the value of nnz
, but not that of nzmax
.
However, you can add as many nonzero elements to the matrix as desired. You are not constrained by the original value of nzmax
.
Importing Sparse Matrices from Outside MATLAB | Viewing Sparse Matrices Graphically |
© 1994-2005 The MathWorks, Inc.