MATLAB: Sparse matrix memory usage clarification

memorysparse

I am trying to understand the memory usage of a sparse matrix.
I have read the following:
Strategies for Efficient Use of Memory:
Sub-Topic : Make Arrays Sparse When Possible
Basic calculation for memory consumption :
In general, for a sparse double array with nnz nonzero elements and ncol columns, the memory required is
– 16 * nnz + 8 * ncol + 8 bytes (on a 64 bit machine)
– 12 * nnz + 4 * ncol + 4 bytes (on a 32 bit machine)
where
nnz = Number of non-zeros
ncol = Number of columns of the Sparse Matrix
I have a 64bit machine running 2019b.
I have the following code that does not follow the basic calculation for the above, namely, nnz does not seem to matter here.
For a sparse matrix of 6×6, after more than 1 element, the memory usage seem to remain stagnant at 232 bytes with increasing elements (until a dense enough matrix).
It seems that the calculation above does not match what I have obtained via whos for the memory of the sparse matrix.
Please advise on what I am missing.
Thank you!
a = sparse(6,6);
a(1,1)=1;
a(2,1) = 2;
a(1,2)=2;
a(2,2)=-1;
a(3,2)=2;
a(2,3)=2;
a(3,3)=-1;
a(4,3)=2;
a(3,4)=2;
a(4,4)=1;
a(6,6)=3;
whos("a")
Name Size Bytes Class Attributes
a 6x6 232 double sparse
a = sparse(6,6);
a(1,1)=1;
whos("a")
Name Size Bytes Class Attributes
a 6x6 72 double sparse
a = sparse(6,6);
a(1,1)=1;
a(2,1) = 2;
whos("a")
Name Size Bytes Class Attributes
a 6x6 232 double sparse

Best Answer

MATLAB allocates a larger memory than nnz for sparse matrix, it's call nzmax, with some strategy that is not documented. You should not count with nnz but with nzmax.
>> a=sparse(6,6);
>> a(1,1)=1
a =
(1,1) 1
>> whos a
Name Size Bytes Class Attributes
a 6x6 72 double sparse
>> nzmax(a)
ans =
1
>> a(2,1)=2
a =
(1,1) 1
(2,1) 2
>> whos a
Name Size Bytes Class Attributes
a 6x6 232 double sparse
>> nzmax(a)
ans =
11
>>