Hi Guys,
I am trying to optmimise this code so that it runs in under 10 seconds for N=20k.
It currently takes around 40 seconds to run.
I think that I need to vecotirse some or all of the loops so that the calculations are done at the same time, but I cannot figure out how to do it.
Any help would be much appreciated as we are learning from home with next to no support from the University.
Here is the code.
s = zeros(N,N);for n = 1:3 for a = 1:N count = 0; for b=1:N off=0; if (pos(n,a)<=0.25 && pos(n,b) >= 0.75) off=1; elseif pos(n,a)>=0.75 && pos(n,b)<=0.25 off=-1; end s(a,b)=s(a,b)+(pos(n,a)-pos(n,b)+off)^2.0; if n == 3 s(a,b)=sqrt(s(a,b)); end end endendmatch = zeros(1,N);for a=1:N mindist=1e10; for c=1:N if (a~=c) mindist=min(s(a,c),mindist); if (mindist==s(a,c)) match(a)=c; end end endend
Best Answer