Allocate a big matrix
이전 댓글 표시
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
KSSV
2017년 1월 10일
What is the purpose of allocating a matrix?
Louis Wyss
2017년 1월 10일
James Tursa
2017년 1월 10일
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
2017년 1월 11일
편집: Walter Roberson
2017년 1월 16일
채택된 답변
추가 답변 (1개)
Jan
2017년 1월 10일
1 개 추천
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
Louis Wyss
2017년 1월 10일
편집: Walter Roberson
2017년 1월 10일
Louis Wyss
2017년 1월 10일
Walter Roberson
2017년 1월 10일
What are some typical value for xTransducer(l) and xiTransKoor(i,j,k) ? So that we can test our ideas?
Louis Wyss
2017년 1월 10일
Walter Roberson
2017년 1월 10일
Are any of your transducer values or xiTransKoor values complex valued? Because if not then the abs() part is not needed because of the ^2.
Walter Roberson
2017년 1월 10일
The vectorized form of your calculation appears to be:
yT2 = A*exp(-abs(bsxfun(@minus, xTransducer(:), reshape(xiTransKoor, [1 size(xiTransKoor)])).^2/a^2);
yT2 = reshape(yT2, numTrans*numWinkel, numPixZ*numPixX);
yT2(yT2 < 1E-50) = 0;
However that last step ends up building a temporary logical array the same size as the original, which could take a fair bit of memory. If you don't need the small values set to 0, then omit that last step. If you do need the small values set to 0, then you can reduce the temporary memory by using a for loop, something like
for K = 1 : size(yT2,2)
yT2( yT2(:,K) < 1E-50, K) = 0;
end
Comments to your code:
- 10^(-50) is an expensive power operation while 1e-50 is a constant.
- Omit the line "yT2((k-1)*numTrans + l,(j-1)*numPixX + i) = 0;", because the elements are set to zeros already.
- Avoid calculating a^2 in each iteration by creating it as a constant before the loops.
- In
Int = A*exp(-(abs(xTransducer(l) - xiTransKoor(i,j,k))^2)/(a^2));
you can omit the abs(), when the values of xTransducer and xiTransKoor are real.
- If you want to work with an 8GB array, prefer installing 16GB or more RAM, as a rule of thumb.
- I assume the bottleneck of your code is the expensive EXP() function. But you do not need this in many cases:
A*exp(-(abs(xTransducer(l) - xiTransKoor(i,j,k))^2)/(a^2)) < 1e-50
==> log(A) - (xTransducer(l) - xiTransKoor(i,j,k))^2/a^2 < log(1e-50)
==> abs(xTransducer(l) - xiTransKoor(i,j,k)) < sqrt((-log(1e-50) + log(A)) * a^2)
Then your code can be:
numTrans = 128;
numWinkel = 720;
numPixX = 100;
numPixZ = 100;
yT2 = zeros(numTrans*numWinkel,numPixZ*numPixX); % sparse!
a2 = a^2;
c = sqrt((-log(1e-50) + log(A)) * a2);
for k = 1:length(phi)
for l = 1:numTrans
for j = 1:numPixX
for i = 1:numPixZ
d = abs(xTransducer(l) - xiTransKoor(i,j,k));
if d >= c
Int = A * exp(-d^2 / a2);
yT2((k-1)*numTrans + l,(j-1)*numPixX + i) = Int;
end
end
end
end
end
Please check the calculations carefully, it is late in the night here. But if the output array conatins many zeros, reducing the number of expensive EXP() calls will accelerate the code remarkably.
Louis Wyss
2017년 1월 11일
Louis Wyss
2017년 1월 12일
편집: Walter Roberson
2017년 1월 12일
Jan
2017년 1월 15일
Please open a new thread for a new question.
카테고리
도움말 센터 및 File Exchange에서 Sparse Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!