MATLAB Answers


How can I vectorize this function with nested FOR loop?

Asked by abtin irani on 2 Jul 2019
Latest activity Edited by Matt J
on 5 Jul 2019
I have two for loops.L is one matrix of random numbers between 0,1 with dimension 24*100000.I want to vectorize it but i can't. because current code is very slow and take a long time.please help me.
for i=1:100000
for j=1:100000


This is what is going to make it difficult to vectorize. I believe there is a command to calculate this for you, but I'm not sure what exactly it is. It might be easier to do some research for this specifically, rather than the vectorization.
Why do you want to vectorize the code? There is no general benefit in doing this. Does the current code run too slow? Then an acceleration is the way to go. Vectorizing can improve the speed, but this is not in general.
To optimize the code, we need the chance to run it. Without meaningful input arguments, this is hard. So please provide L.
norm(x)^2 calculates an expensive square root only to square the result afterwards.
Do oyu have the parallel processing toolbox? A parfor might be very useful.

Sign in to comment.




2 Answers

Matt J 님의 답변 2 Jul 2019
Matt J 님이 편집함. 2 Jul 2019
 채택된 답변

Using mat2tiles
normL=mat2tiles(vecnorm(L,2,1), [1, chunksize]);
for i=1:N
for j=1:N
Tc{i,j}=sum( exp(-4*E.^2) ,1);
T=sum( cell2mat(Tc) ,1).'-1;


Do not pass a matrix with 100000 rows to pdist2. This will attempt to create a 100000 x 100000 matrix, which the error message tells you is 75GB!!
so,what can i do if i don't use there any other solution instead of pdist2 that doesn't give this error? i want one solution for chunksize=1000000. thank you for your answers.
Well, maybe the question to ask is, why are you making the chunksize larger, when you toldu us that the code already worked when the chunksize was just 10,000? Why change/fix something that is not broken?

Sign in to comment.

Jan 님의 답변 4 Jul 2019

n = 1000;
L = rand(24, n);
T = zeros(n, 1);
for i=1:n
K = exp(-4*sum((L(:,i) - L) .^ 2, 1) ./ sum(L(:,i).^2, 1));
T2(i) = sum(K, 2) - 1;
This is 100 times faster than the original version for n=1000.
With parfor instead of for a further acceleration is possible.
To my surprise the above code is faster than this, which omits the repeated squaring:
T = zeros(n,1);
L2 = L .^ 2;
for i=1:n
K = exp(-4*sum((L2(:,i) - 2*L(:,i).*L + L2), 1) ./ sum(L2(:,i), 1));
T(i) = sum(K, 2)-1;


excuse me i got this error. Error using .*
Matrix dimensions must agree.
Error in Untitled8 (line 33)
K=exp(-4*sum((L2(:,i) - 2*L(:,i).*L + L2), 1) ./ sum(L2(:,i), 1));
You use Matlab < R2016b. Then:
K = exp(-4*sum((L2(:,i) - bsxfun(@times, 2*L(:,i), L) + L2), 1) ...
./ sum(L2(:,i), 1));
But the first version seems to be faster.

Sign in to comment.