0

I am trying to store a matrix of size 4 x 10^6, but the Matlab can't do it when running it, it's like it can't store a matrix with that size or I should use another way to store. The code is as below:

matrix = [];
    for j = 1 : 10^6 
        x = randn(4,1); 
        matrix = [matrix x]; 
    end 

The problem it still running for long time and can't finish it, however when I remove the line matrix = [matrix x]; , it finishes the loop very quickly. So what I need is to have the matrix in file so that I can use it wherever I need.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Gze
  • 398
  • 1
  • 12
  • Your loop does the same as `matrix = randn(4,1e6)`. Because you are not allocating any memory for the matrix, the loop takes a lot of time, see [preallocation](https://nl.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html). As for the maximum size of matrix, this depends on how much RAM you have available. – rinkert Nov 08 '19 at 10:12

1 Answers1

4

It is determined by your amount of available RAM. If you store double values, like here, you require 64 bits per number. Thus, storing 4M values requires 4*10^6*64 = 256M bits, which in turn is 32MB RAM.

A = rand(4,1e6);
whos A
  Name      Size                    Bytes  Class     Attributes

  A         4x1000000            32000000  double  

Thus you only cannot store this if you have less than 32MB RAM free.

The reason your code takes so long, is because you grow your matrix in place. The orange wiggles on the line matrix = [matrix x]; are not because the festive season is almost here, but because it is very bad practise to do this. As the warning tells you: preallocate your matrix. You know how large it will be, so just initialise it as matrix = zeros(4,1e6); instead of growing it.

Of course in this case you can simply do matrix = rand(4,1e6), which is even faster than looping.


For more information about preallocation see the official MATLAB documentation, this question (which I answered), or this one.

Adriaan
  • 17,741
  • 7
  • 42
  • 75