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?"

Answers (2)

Matt J
Matt J on 10 Feb 2024
Edited: Matt J on 10 Feb 2024
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,
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 Comments

I used sparse matrices but it's not enough. Numbers like 60000x60000 or even more
That's not very large for a sparse matrix, unless you are telling us that most of those 60000x60000 are non-zero.
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.

Sign in to comment.

Products

Release

R2023b

Asked:

on 10 Feb 2024

Commented:

on 10 Feb 2024

Community Treasure Hunt

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

Start Hunting!