out of memory error when using diag function in ma

2019-08-07 14:14发布

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?

3条回答
女痞
2楼-- · 2019-08-07 14:54

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!

查看更多
对你真心纯属浪费
3楼-- · 2019-08-07 15:02

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.

查看更多
冷血范
4楼-- · 2019-08-07 15:14

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

查看更多
登录 后发表回答