Effacer les filtres
Effacer les filtres

why "while loop" is not working?

2 vues (au cours des 30 derniers jours)
Artur Iskhakov
Artur Iskhakov le 3 Juil 2020
Commenté : Artur Iskhakov le 3 Juil 2020
Hi! My task is to find all nodes that go after node "3" in directed graph "gr". In order to make it I used the "successors" function and while loop. But while loop is not working. Can anybody help me please? Here's the code:
clc;
A = [1 2 3 4 5 6 7];
B = [2 3 4 5 6 7 8];
w = [0 0 1 0 0 0 0];
gr = digraph (A,B, w);
N = successors (gr,4)
i=0;
while TF == 0
TF = isempty (N);
N = successors (gr, N);
i = i + 1;
N(i)= N;
end
  2 commentaires
madhan ravi
madhan ravi le 3 Juil 2020
What’s TF ?
Artur Iskhakov
Artur Iskhakov le 3 Juil 2020
It is a variable that equals to logical 1 when matrix "N" is empty and to logical 0 when matrix "N" is not empty.

Connectez-vous pour commenter.

Réponse acceptée

Vashist Hegde
Vashist Hegde le 3 Juil 2020
To check what was going on, I wrote a small while loop on my own -
This while loop is also not working. The similarity between this code and your code is that you have not initialized TF. so when I put break points, and ran this stepwise, I found that when a variable is not initialized and is used, it is by default set to 1.
So TF has a value of 1, and hence the loop is never entered.
I suggest you to initialize TF.
while TF==0
disp('hi');
TF=1;
var = randi(5);
if var==4
TF=3;
end
end
  1 commentaire
Artur Iskhakov
Artur Iskhakov le 3 Juil 2020
thank you! )))

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construction 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