out of memory error when write matrix

"I need to define a matrix A=zeros(N,N) where N is a very large number.
Is there a way to write the matrix without encountering the out of memory error?"

回答 (2 件)

Matt J
Matt J 2024 年 2 月 10 日
編集済み: Matt J 2024 年 2 月 10 日

0 投票

You could make it sparse,
A=sparse(N,N);
but be mindfult that if you then want to populate the A(i,j) with non-zero numbers, it will be very slow to do so one at a time. The more efficient way to populate a sparse matrix is,
Steven Lord
Steven Lord 2024 年 2 月 10 日
移動済み: Steven Lord 2024 年 2 月 10 日

0 投票

How large a "very large number" are we talking about?
Such a matrix would require a block of contiguous memory 8*N^2 bytes in size (assuming full real double.)
For some values of N, this is easily achievable.
N = 10;
A = zeros(N);
For some values of N, it's theoretically possible depending on your machine and what else you have running on it.
For some values of N, it's technically theoretically possible but there's no machine with enough memory to create it.
For some values of N, it's not even theoretically possible.
[~, maxelements] = computer;
N = ceil(sqrt(maxelements))
N = 16777216
A = zeros(N);
Error using zeros
Requested array exceeds the maximum possible variable size.
If you do need to work with data that's that large, using sparse matrices (as Matt J suggested) is one option. The tools listed in this section of the documentation provides other options.

3 件のコメント

FRANCESCO MICELI
FRANCESCO MICELI 2024 年 2 月 10 日
I used sparse matrices but it's not enough. Numbers like 60000x60000 or even more
Matt J
Matt J 2024 年 2 月 10 日
That's not very large for a sparse matrix, unless you are telling us that most of those 60000x60000 are non-zero.
Steven Lord
Steven Lord 2024 年 2 月 10 日
So how large a contiguous block of memory would a real full double matrix that size require?
N = 60000;
bytes = 8*N^2;
gb = bytes/(1024^3)
gb = 26.8221
That's pretty large. Let's check my calculations by asking MATLAB Answers to create such a matrix.
A = zeros(N);
Error using zeros
Requested 60000x60000 (26.8GB) array exceeds maximum array size preference (5.0GB). This might cause MATLAB to become unresponsive.
Perhaps if you tell us your application we may be able to offer some suggestions for alternate ways to solve the problem without requiring creating such a large matrix.

サインインしてコメントする。

カテゴリ

製品

リリース

R2023b

質問済み:

2024 年 2 月 10 日

コメント済み:

2024 年 2 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by