how can I write a while condition for an empty array?
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I write a while loop as:
while set==[]
....
....
end
but when I run my code it only works until the while loop, not enters inside the loop and on the text of the while set==[] there is an error message as: operator '[' is seldom used in a scalar context.
What is the problem? I want my loop run until my set array has no element. If required I can send my code...
Thanks in advance... Regards...
2 Commenti
Stephen23
il 25 Mag 2015
Do NOT name any variable set, as this is the name of a very important inbuilt function set. Using the names of inbuilt functions and values is a very bad idea. You should always check if the name exists using which name
Risposte (1)
Stephen23
il 25 Mag 2015
Modificato: Stephen23
il 25 Mag 2015
while isempty(value)
...
end
4 Commenti
Stephen23
il 25 Mag 2015
Modificato: Stephen23
il 25 Mag 2015
Because the first row is all zeros, the while loop will never begin:
pre= [0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 1 0 0 0 0 0 0 0 0 0;
1 0 1 1 1 0 0 0 0 0 0;
1 1 0 0 0 1 0 0 0 0 0;
1 0 1 1 1 0 1 0 0 0 0;
1 1 0 0 0 1 0 1 0 0 0;
1 0 1 1 1 0 1 0 1 0 0];
AA = find(all(pre==0,2))
when I run this it displays this in my command window:
AA =
1
so AA is not empty, and your while loop will never start. Perhaps you need to fix the algorithm that you are using.
Suggestion: instead of trying to fix broken code, if you actually tell us exactly what you are trying to achieve then we could advise some neat and efficient ways to achieve this.
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!