Reputation: 125
Is there an effective way to declare a very big matrix (let's say 40.000.000x10) of integers in Matlab? If I do it like this:
var=uint8(zeros(40000000,10));
It works very well in command window. But the same code works much worse in function! If I do this somewhere in the function, it firstly creates a 40.000.000x10 matrix of doubles and then converts it to 8-bit integers matrix. I would prefer if it was created as integer matrix from the very begin, as in commands window. I have to work with even bigger matrices and I ran out of RAM when it initializes such matrix of doubles (although there would be enough memory if it initialized the matrix as integers). And I don't really need doubles here, all numbers are in range 0:100. Hope you understood the problem :D
Upvotes: 1
Views: 3834
Reputation: 18484
If you want to be a bit tricky and save a small amount of time, you can allocate your array of uint8
zeros this way:
var(40000000,10) = uint8(0);
See here for some details on this type of preallocation. Be careful with this scheme. If you allocate var
as one size and then allocate it again without clearing it as a smaller size array using this method, the size won't actually change and the data won't be zeroed out. Essentially, this scheme is only good if the array (var
here) doesn't exist yet.
Upvotes: 0
Reputation: 1067
Maybe you should use this, which is more efficient.
var = zeros(40000000, 10, 'uint8');
Upvotes: 0