out of memory error when using diag function in matlab

房东的猫 提交于 2019-12-11 06:38:07

问题


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?


回答1:


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:

diag(M)(a, b) =
                   M(a)    | a == b
                   0       | a != b

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 and M(n) = An




回答2:


Use saprse matrix

 M = spdiags( M, 0, numel(M), numel(M) );

For more info see matlab doc on spdiags and on sparse matrices in general.




回答3:


If you have an n-by-n square matrix, M, you can directly extract the diagonal elements into a row vector via

n = size(M,1);    % Or length(M), but this is more general
D = M(1:n+1:end); % 1-by-n vector containing diagonal elements of M

If 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 of M and can get rid of the rest, you can do this:

M(:) = 0;         % Zero out M
M(1:n+1:end) = D; % Insert diagonal elements back into M
clear D;          % Clear D from memory

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 need n^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!



来源:https://stackoverflow.com/questions/16971111/out-of-memory-error-when-using-diag-function-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!