Can someone tell me what I am doing wrong with this loop?

1 vue (au cours des 30 derniers jours)
pat2ondo
pat2ondo le 25 Nov 2015
Commenté : Thorsten le 25 Nov 2015
Ps keeps looping. I just want to stop at mstlength which is 11.
for l=1:mstlength
ls = mst(l,1);
for p=1:mstlength
ps = mst(p,2);
plot([nodes(ls,2) nodes(ps,2)],[nodes(ls,3) nodes(ps,3)],'k.-')
hold on
end
end
Thank you

Réponses (1)

Thorsten
Thorsten le 25 Nov 2015
Modifié(e) : Thorsten le 25 Nov 2015
Do you want 11 runs? Then just use one loop
for l=1:mstlength
ls = mst(l,1);
ps = mst(p,2);
plot...
end
Otherwise there's nothing wrong with your loops, l runs from 1 to 11 and for each l, p runs from 1 to 11:
>> mstlength = 11;
for l=1:mstlength
%ls = mst(l,1);
for p=1:mstlength
% ps = mst(p,2);
disp(sprintf('l = %d, p = %d', l, p))
%plot([nodes(ls,2) nodes(ps,2)],[nodes(ls,3) nodes(ps,3)],'k.-')
%hold on
end
end
l = 1, p = 1
l = 1, p = 2
l = 1, p = 3
l = 1, p = 4
l = 1, p = 5
l = 1, p = 6
l = 1, p = 7
l = 1, p = 8
% more lines here...
l = 11, p = 8
l = 11, p = 9
l = 11, p = 10
l = 11, p = 11
  2 commentaires
pat2ondo
pat2ondo le 25 Nov 2015
Thank for your answer. The first one did work.
for l=1:mstlength,
text(nodes(l,2),nodes(l,3),[' ' num2str(ids(l))],'Color','b','FontWeight','b')
ls = mst(l,1);
ps = mst(l,2)
plot([nodes(ls,2) nodes(ps,2)],[nodes(ls,3) nodes(ps,3)],'k.-')
hold on
end;
Thorsten
Thorsten le 25 Nov 2015
Fine. If you are happy, please accept my answer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by