How much memory a Sparse Matrix created using SPRAND, with given number of rows, columns and density consume in MATLAB 7.14 (R2012a)?

1 view (last 30 days)
If I know the number of rows (say m) and the number of columns (say n) of a sparse matrix, to be created using SPRAND with density (say rho). How much is the memory consumed with such a matrix and also are there any guidelines as to when use SPARSE matrices compared to standard matrix?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Nov 2020
Edited: MathWorks Support Team on 5 Nov 2020
In MathWorks documentation, there exists a formula to compute this :
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
Also, note SPARSE matrices are stored in a different fashion compared to standard matrices in MATLAB, hence under certain circumstances SPARSE matrices may consume more memory compared to standard matrices.
As a guideline you can follow this :
* If the memory consumption (computed using the formula above) > 8*NumOfRows*NumOfColumns => then it is not recommended.
The memory consumption and the proper usage of Sparse Matrices can be written as a MATLAB function as :
function tdensity(m,n,rho)
if nargin == 3
nnzS = m * n * rho
bytesS64 = 16*nnzS + 8*(n+1)
% bytesS32 = 12*nnzS + 4*(n+1)
bytesA = 8 * m * n
if bytesS64 > bytesA
disp('It is not worthwhile to store your matrix in sparse format, on 64-bit platforms')
else
disp('It is worthwhile to store your matrix in sparse format, on 64-bit platforms')
end
else
maxnnzS64 = (8*m*n - 8*(n+1)) / 16
disp(sprintf('The maximum number of nonzeros to make it worthwhile to store your matrix in sparse format on 64-bit platforms is %d',maxnnzS64))
end

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2012a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!