MATLAB: Allocate a big matrix

allocatearraymatrixzeros

I want to allocate a (10^5,10^4) matrix and after I want to fill the matrix in a for-loop with values. My problem is, that zeros(10^5,10^4) gives the error "matrix exceeds maximum array size preference." and sparse(10^5,10^4) makes the for-loop to slow. Has someone of you a solution of this problem?
Thanks a lot

Best Answer

Use spalloc() specifying the number of expected non-zero values.
Sparse matrices are more efficiently filled working down columns than across rows.
10^5 by 10^4 is 10^9, just under 1 giga-entries. If you were to use the default double() datatype, that would require 8 gigabytes to store. Your preferences have been configured to not allow you to store matrices that large. If you have enough memory for this (and for the other arrays you are using) then you could change your preferences. And if you can get away with using something other than double precision, then create the array that way. For example,
A = zeros(10^5, 10^4, 'int16');
would take only 1/4 of the storage.
Related Question