Asked by abtin irani
on 2 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.

K=zeros(100000,1);

T=zeros(100000,1);

for i=1:100000

for j=1:100000

K(j,1)=exp(-4*norm(L(:,i)-L(:,j))^2/norm(L(:,i))^2);

end

T(i,:)=sum(K)-1;

end

Answer by Matt J
on 2 Jul 2019

Edited by Matt J
on 2 Jul 2019

Accepted Answer

Using mat2tiles

chunksize=10000;

Lc=mat2tiles(L.',[chunksize,24]);

normL=mat2tiles(vecnorm(L,2,1), [1, chunksize]);

N=numel(Lc);

Tc=cell(N);

for i=1:N

for j=1:N

E=pdist2(Lc{i},Lc{j})./normL{j};

Tc{i,j}=sum( exp(-4*E.^2) ,1);

end

end

T=sum( cell2mat(Tc) ,1).'-1;

Matt J
on 3 Jul 2019

abtin irani
on 4 Jul 2019

Matt J
on 4 Jul 2019

Sign in to comment.

Answer by Jan
on 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;

end

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;

end

abtin irani
on 4 Jul 2019

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));

Jan
on 4 Jul 2019

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.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Bob Nbob (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469855-how-can-i-vectorize-this-function-with-nested-for-loop#comment_720464

## Jan (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/469855-how-can-i-vectorize-this-function-with-nested-for-loop#comment_720504

Sign in to comment.