Allocate a big matrix
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Louis Wyss
el 10 de En. de 2017
Editada: Walter Roberson
el 16 de En. de 2017
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
4 comentarios
James Tursa
el 10 de En. de 2017
You should not be filling in a sparse matrix in a for-loop. The usual technique advised is to save the values and indexes in separate variables and then combine into a sparse matrix at the end. Can you show us what your for-loop looks like?
Louis Wyss
el 11 de En. de 2017
Editada: Walter Roberson
el 16 de En. de 2017
Respuesta aceptada
Walter Roberson
el 10 de En. de 2017
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.
3 comentarios
Walter Roberson
el 10 de En. de 2017
If you initialize as int16 and write double values then the double values will be converted to integers.
If you need to store double, then you cannot use this technique.
Are you trying to do something like subpixel resolution? If so then you simply do not have enough memory to work at that resolution.
Más respuestas (1)
Jan
el 10 de En. de 2017
You can increase the accepted size of allocated arrays in Matlab's preferences. If you have enough RAM to allocate an array of 8GB, there is no reason to restrict the arrays to smaller sizes.
Did you try to allocate the sparse matrix with a matching number of non-zero elements? Are you sure that the low speed of the loop is a problem of the sparse array? Perhaps there are other reasons. Perhaps somebody has a suggestion for improvements, when you post the relevant part of the code.
Ver también
Categorías
Más información sobre Performance and Memory en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!