# Convert from sparse to full matrix

In MATLAB I have found a function full which converts a matrix from a sparse matrix to a full matrix.

I know that sparsity of a matrix is the fraction of zero-elements compared to total number of elements, but I don't know how to convert between full and sparse matrices.

Is a sparse matrix a non-quadratic matrix since some of the rows can be eliminated due to being a row with only zero elements?

## 1 Answer

The difference between a full and a sparse matrix in MATLAB is not really a mathematical one (because all calculations should work correctly either way), but more a matter of how the entries of the matrix are stored.

The MATLAB documentation links to this paper for more information and the following example is given there:

The command

A = [0 0 11; 22 0 0; 0 33 0]


generates a full matrix, which can be displayed as

A =
0     0   11
22    0    0
0    33    0


This means that all the zeros are actually stored explicitly in memory, because also the number $0$ needs the same amount of memory as every other number does.

If the full matrix is transformed to a sparse matrix with the command

S = sparse(A)


it is displayed (and stored in memory) as

S =
(2,1)   22
(3,2)   33
(1,3)   11


You can see here that only the nonzero entries of the matrix and in addition their positions inside the matrix are stored, whereas the zero elements are ignored and all entries of the matrix which are not present in the $(i,j)$-coordinates are implicitly assumed to be zero.

If a matrix has a large number of zeros, this way of storing it saves a lot of memory: If you have a $n\times n$ -matrix with $m$ nonzero elements, it needs $n^2$ units of memory to be stored as a full matrix, but $3m$ units of memory to be stored as a sparse matrix (assuming that storing the positions $(i,j)$ needs 2 units of memory). If $m$ is much smaller than $n^2$, i.e. the matrix is sparse, then you will see that $3m$ is of course also much smaller than $n^2$.