How can I pre allocate the memory for a variable? or is it possible?

2 views (last 30 days)
Assume that I have a variable "Sis" and it will be used as sparse matrix. and non zero elements will be set in the following loops. The problem is if you don't allocate the memory before you use, it takes really long time to create a matrix of (2.000.000, 250.000);
İs there a way to doing that
Sis 33489x5625 3970464 double sparse
Sis_2 33489x5625 1507005000 double
Here is an example to show why I need sparse matrix.
Thanks

Accepted Answer

John D'Errico
John D'Errico on 26 Feb 2014
Edited: John D'Errico on 26 Feb 2014
Why has nobody yet suggested spalloc, i.e., a tool designed to allocate memory for a sparse matrix! That is essentially what was requested. spalloc creates an all zero sparse matrix (like sparse can do), but with space allocated for as many elements as you specify.
Regardless, it is a TERRIBLE idea to build a sparse matrix incrementally, even if it is already allocated as such. Stuffing elements into the matrix inside a loop, even if the space is already allocated, will cause MATLAB to re-order the elements. This will STILL take a lot of time to accomplish, unless you are careful enough to add the elements in the proper order. Even so, my guess is there is still a time penalty.
Far better? Create the matrix in ONE step, using sparse. Do so by creating a list of the elements, as well as their row and column indexes in advance. Only when you are all done do you call sparse. If the matrix is representable as a set of diagonals, then learn to use spdiags. Again, only one call.
Be careful of course. Do not build up that list of elements incrementally either, appending them to the end of an array. That also has quadratic time to create that list, since MATLAB must reallocate the array after each append step. If you know how many elements will be created, then preallocate the memory for the list. Then insert your elements one at a time into the list.
If you do not know how many final elements are in the list, then use a tool for incremental array growth like those I have posted here on the file exchange.
In general, build your sparse matrices in one call, or at least as few calls to sparse as possible. There are some nice tricks you can use, some of which you would learn from tools like inpaint_nans , or blktridiag.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 26 Feb 2014
Sis=zeros(33489,5625)
  1 Comment
Metin Ertas
Metin Ertas on 26 Feb 2014
This really does not work.That s why I added if I used a zeros matrix the memory I need is in the order of 10^9. But what I need is really less than that.That s why I use sparse matrix.

Sign in to comment.


Iain
Iain on 26 Feb 2014
lf you want is to preallocate a sparse array, all you need is:
Sis = sparse(2000000, 250000);
If you look at the help documentation for the sparse function, you might be able to find a better way of doing what you're trying to do.
  1 Comment
Metin Ertas
Metin Ertas on 26 Feb 2014
I did before I start.But this allocates only a really small amount. However when you are working with data like (2000000, 250000) you really need more than that. and it takes around 20 hours.
I ll have a look at sparse documentation for pre allocating.Thanks

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!