Conversion of for loop to Vector.
显示 更早的评论
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 个评论
Guillaume
2015-12-16
An explanation of what the code does (hint: code should have comments) would go a long way towards you getting an answer.
azizullah khan
2015-12-16
Guillaume
2015-12-16
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.
采纳的回答
更多回答(1 个)
Renato Agurto
2015-12-16
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 个评论
azizullah khan
2015-12-16
Guillaume
2015-12-16
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!