I have an array of valued double M where size(M)=15000
I need to convert this array to a diagonal matrix with command diag(M)
but i get the famous error out of memory
I run matlab with option -nojvm to gain memory space
and with the optin 3GB switch on windows
i tried also to convert my array to double precision
but the problem persist
any other idea?
If you have an
n
-by-n
square matrix,M
, you can directly extract the diagonal elements into a row vector viaIf you have an older version of Matlab, the above may even be faster than using
diag
(if I recall,diag
wasn't always a compiled function). Then, if you need to save memory and only need the diagonal ofM
and can get rid of the rest, you can do this:This should not allocate much more than about
(n^2+n)*8 = n*(n+1)*8
bytes at any one time for double precision values (some will needed for indexing operations). There are other ways to do the above that might save a bit more if you need a (full, non-sparse)n
-by-n
diagonal matrix, but there's no way to get around that you'll needn^2*8
bytes at a minimum just to store the matrix of doubles.However, you're still likely to run into problems. I'd investigate sparse datatypes as @user2379182 suggests. Or rework you algorithms. Or better yet, look into obtaining 64-bit Matlab and/or a 64-bit OS!
Use saprse matrix
For more info see matlab doc on
spdiags
and on sparse matrices in general.There are much better ways to do whatever you're probably trying to do than generating the full diagonal matrix (which will be extremely sparse).
Multiplying that matrix, which has 225 million elements, by other matrices will also take a very long time.
I suggest you restructure your algorithm to take advantage of the fact that:
You'll save a huge amount of time and memory and whoever is paying you will be happier.
This is what a diagonal matrix looks like:
Every entry except those along the diagonal of the matrix (the ones where row index equals the column index) is zero. Relating this example to your provided values,
diag(M) = A
andM(n) = An