Allocate a big matrix
Show older comments
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 Comments
KSSV
on 10 Jan 2017
What is the purpose of allocating a matrix?
Louis Wyss
on 10 Jan 2017
James Tursa
on 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
on 11 Jan 2017
Edited: Walter Roberson
on 16 Jan 2017
Accepted Answer
More Answers (1)
Jan
on 10 Jan 2017
1 vote
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 Comments
Louis Wyss
on 10 Jan 2017
Edited: Walter Roberson
on 10 Jan 2017
Louis Wyss
on 10 Jan 2017
Walter Roberson
on 10 Jan 2017
What are some typical value for xTransducer(l) and xiTransKoor(i,j,k) ? So that we can test our ideas?
Louis Wyss
on 10 Jan 2017
Walter Roberson
on 10 Jan 2017
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
on 10 Jan 2017
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
on 11 Jan 2017
Louis Wyss
on 12 Jan 2017
Edited: Walter Roberson
on 12 Jan 2017
Jan
on 15 Jan 2017
Please open a new thread for a new question.
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!