2
votes

Today I have the need to allocate a vector with size 100000 in Matlab. I try to do it simply using:

a=ones(100000);

which my Matlab angrily answered with:

Out of memory. Type HELP MEMORY for your options.

Which is strange since I have Matlab 64 bit running on a 64 bit machine with 8 GB RAM. I tried many of the "resolving out of memory errors in Matlab" recipe in SO or other places but no luck so far.

Now I'm more confused when something like:

a=ones(10000,10000);

Runs without problem in my machine.

Does this mean that Matlab have some mechanism to limit the number of elements of a vector in a single-dimensional space?

3
a=ones(100000); is equivalent to a=ones(100000,100000);Divakar
That seems right! Any ideas so a=ones(100000); can be created within the memory limit?troll
See if this helps.Divakar
Turns out I've the wrong concept about matlab syntax in the first place. Thanks for your help, Divakar.troll

3 Answers

5
votes

Today I have the need to allocate a vector with size 100000 in Matlab.

Now, as noted in the comments and such, the method you tried (a=ones(100000);) creates a 100000x100000 matrix, which is not what you want.

I would suggest you try:

a = ones(1, 100000);

Since that creates a vector rather than a matrix.

3
votes

Arguments Matter

Calling Matlab's ones() or zeros() or magic() with a single argument n, creates a square matrix with size n-by-n:

>> a = ones(5)
  a = 1 1 1 1 1 
      1 1 1 1 1 
      1 1 1 1 1 
      1 1 1 1 1 
      1 1 1 1 1

Calling the same functions with 2 arguments (r, c) instead creates a matrix of size r-by-c:

>> a = ones(2, 5)
  a = 1 1 1 1 1 
      1 1 1 1 1 

This is all well documented in Matlab's documentation.

Size Matters Too

Doubles

Having said this, when you do a = zeros(1e6) you are creating a square matrix of size 1e6 * 1e6 = 1e12. Since these are doubles the total allocated size would be 8 * 1e12 Bytes which is circa (8 * 1e12) / 1024^3 = 7450.6GB. Do you have this much RAM on your machine?

Compare this with a = zeros(1, 1e6) which creates a column-vector of size 1 * 1e6 = 1e6, for a total allocated size of (8 * 1e6) / 1024^3 = 7.63MB.

Logicals

Logical values, on the other hand are boolean values, which can be set to either 0 or 1 representing False or True. With this in mind, you can allocate matrices of logicals using either false() or true(). Here the same single-argument rule applies, hence a = false(1e6) creates a square matrix of size 1e6 * 1e6 = 1e12. Matlab today, as many other programming languages, stores bit values, such as booleans, into single Bytes. Even though there is a clear cost in terms of memory usage, such a mechanism provides significant performance improvements. This is because it is accessing single bits is a slow operation.

The total allocated size of our a = false(1e6) matrix would therefore be 1e12 Bytes which is circa 1e12 / 1024^3 = 931.32GB.

2
votes

Well the first declaration tries to build a matrix of 1000000x1000000 ones. That would be ~931 GB.

The second tries to declare a matrix of 10000 x 10000. That would be ~95MB.

I assumed each one is stored on a byte. If they use floats, than the requested memory size will be 4 times larger.