Allocate a big matrix

6 vues (au cours des 30 derniers jours)
Louis Wyss
Louis Wyss le 10 Jan 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 commentaires
James Tursa
James Tursa le 10 Jan 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
Louis Wyss le 11 Jan 2017
Modifié(e) : Walter Roberson le 16 Jan 2017
numTrans = 128;
numWinkel = 720;
numPixX = 100;
numPixZ = 100;
yT2 = zeros(numTrans*numWinkel,numPixZ*numPixX);
for k = 1:length(phi)
for l = 1:numTrans
for j = 1:numPixX
for i = 1:numPixZ
Int = A*exp(-(abs(xTransducer(l) - xiTransKoor(i,j,k))^2)/(a^2));
if Int < 10^(-50)
yT2((k-1)*numTrans + l,(j-1)*numPixX + i) = 0;
else
yT2((k-1)*numTrans + l,(j-1)*numPixX + i) = Int;
end
end
end
end
end

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Jan 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 commentaires
Walter Roberson
Walter Roberson le 10 Jan 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.
Louis Wyss
Louis Wyss le 10 Jan 2017
I think it is possible a memory problem. Maybe I should try it on a PC with more memory.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 10 Jan 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.
  10 commentaires
Louis Wyss
Louis Wyss le 12 Jan 2017
Modifié(e) : Walter Roberson le 12 Jan 2017
Your recommedations made my program very faster, thanks again for your help. May I ask a second question about invert a matrix? I need to invert the yT2 matrix and I use the tikonov regularization. pinv() is not possible because the reconstruction is very bad when I use pinv(). I tried like in yT2 to split up the calculations but I want to ask you if you know a more efficient way
lambda = 250;
tikhonovMatrix = lambda*speye(numTrans*numWinkel, numPixZ*numPixX);
tikhonovMatrixTrans = tikhonovMatrix';
tikhonovMatrixProd = tikhonovMatrixTrans*tikhonovMatrix;
yT2Trans = yT2';
yT2Prod = yT2Trans*yT2;
pseudoInverse_yT2 = yT2Prod + tikhonovMatrixProd;
bildVektor = reshape(newsinogram,[],1);
recBildVektor = yT2Trans*bildVektor;
recBildVektor2 = pseudoInverse_yT2\recBildVektor;
Jan
Jan le 15 Jan 2017
Please open a new thread for a new question.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by