整段至配件
您可以转换一个完整的矩阵,以利用零散功能进行零散的储存。
S = sparse(A)
例如
A = [ 0 0 0 5
0 2 0 0
1 3 0 0
0 0 4 0];
S = sparse(A)
生产
S =
(3,1) 1
(2,2) 2
(3,2) 3
(4,3) 4
(1,4) 5
The printed output lists the nonzero elements of S, together with their row and column indices. The elements are sorted by columns, reflecting the internal data structure.
You can convert a sparse matrix to full storage using the full function, provided the matrix order is not too large. 例如 A = full(S) reverses the example conversion.
Converting a full matrix to sparse storage is not the most frequent way of generating sparse matrices. If the order of a matrix is small enough that full storage is possible, then conversion to sparse storage rarely offers significant savings.