Main Content

full

Convert sparse matrix to full storage

Syntax

Description

example

A= full(S)converts sparse matrixSto full storage organization, such thatissparse(A)returns logical0(false).

Examples

collapse all

Change the storage format of a matrix and compare the storage requirements.

Create a random sparse matrix. The display of sparse matrices in MATLAB ® omits all zeros and shows the location and value of nonzero elements.

rngdefault%for reproducibilityS = sprand(8,8,0.3)
S = (2,1) 0.0344 (7,1) 0.4456 (8,1) 0.7547 (2,2) 0.4387 (4,3) 0.7655 (7,3) 0.6463 (8,4) 0.2760 (1,6) 0.9502 (5,6) 0.1869 (8,6) 0.6797 (3,7) 0.3816 (4,7) 0.7952 (8,7) 0.6551 (6,8) 0.4898 (7,8) 0.7094

Convert the matrix to full storage. The MATLAB display of the matrix reflects the new storage format.

A = full(S)
A =8×80 0 0 0 0 0 0 0.9502 0 0 0.0344 0.4387 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3816 0 0 0 0 0 0.7655 0 0 0 0 0 0 0 0.7952 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4898 0.44456 0 0.44456 0 0.44456 00.6463 0 0 0 0 0 0.7094 0.7547 0 0 0.2760 0 0.6797 0.6551 0

Compare the storage requirements of the two formats:

  • Auses storage for 64 doubles (8 bytes each), or 64 8 = 512 bytes.

  • Suses storage for 15 nonzero elements, as well as 24 integers describing their positions, for a total of 39 8 = 312 bytes.

whos
Name Size Bytes Class Attributes A 8x8 512 double S 8x8 312 double sparse

Input Arguments

collapse all

Sparse matrix to convert, specified as a matrix. IfSis already a full matrix, thenAis identical toS.

Tips

  • IfXis anm-by-nmatrix withnznonzero elements, thenfull(X)requires space to storem*nelements. On the other hand,sparse(X)requires space to storenz元素和(nz+n+1)integers.

    矩阵的密度(nnz(X)/numel(X)) determines whether it is more efficient to store the matrix as sparse or full. The exact crossover point depends on the matrix class, as well as the platform. For example, in 32-bit MATLAB®, a double sparse matrix with less than about 2/3 density requires less space than the same matrix in full storage. In 64-bit MATLAB, however, double matrices with fewer than half of their elements nonzero are more efficient to store as sparse matrices.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a