Out of memory when use zeros
Mostra commenti meno recenti
hi
i have
Out of memory. Type HELP MEMORY for your options.
hen i want to creat a matrix KG
KG=zeros(3*NNOD,3*NNOD);
NNOD= 2000
i use matlab 2014b in windows32
there is another way to create matrix KG or built KG ithout initialise it to zeros
4 Commenti
David Goodmanson
il 11 Nov 2020
Modificato: David Goodmanson
il 11 Nov 2020
Hi alize
A 6000x6000 matrix of double precision numbers takes 288MB of memory. There are a few ways to bring that down. First, if a small percentage of the matrix elements are actually nonzero then you can go to sparse matrices, which can reduce required memory, basically by the percentage of nonzeros. That could potentially be a huge reduction. If the matrix is full and the numbers are floating point, you could store them as single precision rather than double precision. That's worth a factor of 2, but I would say things are not often done that way. If all the numbers are integers, you could store them as int8 or uint8 or int16 etc ... That improves things by a factor of 8 for int8 for example, although the range of integers that can be stored is of course reduced. See 'help int8' as a gateway to all the possibilities.
James Tursa
il 11 Nov 2020
Even if you can somehow squeeze out barely enough contiguous memory for this, you will have to think of downstream processing. What are you going to do with this variable? It would not be unusual to need 2x - 3x as much memory just to meaningfully manipulate it downstream in your code. What is the problem you are working on? Maybe there is another less memory intensive way to accomplish your goals.
Steven Lord
il 11 Nov 2020
In addition to what others have said, you indicated:
i use matlab 2014b in windows32
A 32-bit version of MATLAB on 32-bit Windows is severely limited in how much memory it can access. If you can switch to a 64-bit version of MATLAB on a 64-bit version of Windows, that may help you out.
alize beemiel
il 12 Nov 2020
Risposte (1)
Image Analyst
il 11 Nov 2020
How are you going to use this array? Are you just going to see if the values are 0 or 1? If so, you can use a logical array and use 8 times less memory:
NNOD = 2000
KG = false(3*NNOD, 3*NNOD); % Using false instead of zeros to create a logical matrix.
Alternatively, if it needs to be floating point, because it has values other than 0 and 1, you can cast it to single instead of double. This will cut the memory used by half:
NNOD = 2000
KG = zeros(3*NNOD, 3*NNOD, 'single'); % Using 'single' to make array single precision instead of double precision.
You can also cast it to int32, int16, uint8, or other types depending on how you're going to use it and what you plan on putting into the matrix.
5 Commenti
alize beemiel
il 12 Nov 2020
Image Analyst
il 12 Nov 2020
You're welcome. Since my idea of making it single worked, could you please "Accept this answer" by clicking the link? Thanks in advance.
David Goodmanson
il 12 Nov 2020
Hello IA,
You have repeated pretty much exactly what I said in one of the earlier comments about using single, uint8 etc, for which you are looking for credit. Fine if you get the credit, but: It may be your idea, but it 's not your idea posted first or exclusively.
James Tursa
il 12 Nov 2020
... which is why I wish we could vote for comments.
Image Analyst
il 12 Nov 2020
David, I didn't see anyone mentioning logical so I added that. And then I saw that no one had given actual code for 'single', which users usually like so they can click the copy button and don't need to look up in the documentation to figure it out, so I added that code for the user's convenience. I suggest you move your comment down to the official "Answers section" since your comment seems more like an actual answer (and a very good one!) than a comment asking the poster for clarification. We're supposed to comment on the poster's original post when we need to ask questions about it to get them to clarify it or add missing info or files, but otherwise put answers further below in the Answers section.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!