Conversion of for loop to Vector.
Ältere Kommentare anzeigen
I have two for loops and it's processing is very slow.I need to speed up the task. Any help would be highly appreciated.
for i=1:ll
for j=1:ll
X=[a(i),b(i);a(j),b(j)];
d = pdist(X,'euclidean');
if d>0 && d<(4.1*ro)
if k2(a(i),b(i))>k2(a(j),b(j))
k3(a(j),b(j))=0;
end;
if k2(a(i),b(i))<k2(a(j),b(j))
k3(a(i),b(i))=0;
end;
end;
end;
end;
Thank you,,,,
3 Kommentare
Guillaume
am 16 Dez. 2015
An explanation of what the code does (hint: code should have comments) would go a long way towards you getting an answer.
azizullah khan
am 16 Dez. 2015
Guillaume
am 16 Dez. 2015
What I meant is that you should have a description of the purpose of each line of code. Ideally, this should be comments in the code itself.
I can guarantee you won't have a clue how the code above does its job if you come back to it in a year.
Akzeptierte Antwort
Weitere Antworten (1)
Renato Agurto
am 16 Dez. 2015
Hello,
I would try this under the assumjption that:
pdist(X1,'euclidean') == pdist(X2,'euclidean')
if:
X1 = [a(i),b(i);a(j),b(j)];
X2 = [a(j),b(j);a(i),b(i)];
Code:
for i=1:ll
for j=i+1:ll
X=[a(i),b(i);a(j),b(j)];
d = pdist(X,'euclidean');
if d>0 && d<(4.1*ro)
if k2(a(i),b(i))>k2(a(j),b(j))
k3(a(j),b(j))=0;
elseif k2(a(i),b(i))<k2(a(j),b(j))
k3(a(i),b(i))=0;
end;
end;
end;
end;
2 Kommentare
azizullah khan
am 16 Dez. 2015
Guillaume
am 16 Dez. 2015
As I've shown in my answer you can calculate the euclidean distance between all points in one go with just one line:
d = hypot(bsxfun(@minus, b, b'), bsxfun(@minus, a, a'))
The loops, the pdist, the if, all of this is unnecessary.
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!