While loop not working (beginner)

7 visualizaciones (últimos 30 días)
Alexis
Alexis el 9 de Dic. de 2022
Respondida: Vinayak Choyyan el 12 de Dic. de 2022
I am trying to count how "long" the array for k_earth is and assign it a variable. height_earth is an array. I need to use some kind of for or while loop. When I run this code:
k_earth = find(height_earth >= 0); % Values where the ball is in the air
while height_earth>= 0
L = length(k_earth);
end
I get the error :
Unrecognized function or variable 'L'.
Error in EGR_115_Final_Project_Hall_Alexis (line 62)
time_impact_earth = time(k) ; % Time when ball impacts
and if I put in a value for L, it does not change after running through the for loop.
  1 comentario
David
David el 9 de Dic. de 2022
Hi! Did you try to assingn 'L' with a default value (Like 1) from out of the loop?

Iniciar sesión para comentar.

Respuesta aceptada

Vinayak Choyyan
Vinayak Choyyan el 12 de Dic. de 2022
Hi Alexis
I have not quite understood what you are trying to achieve.
In your code,
height_earth
is an array and height_earth>=0 will result in a logical array. For example if
height_earth= [1 2 3 4 5 -1 -2 -3]; %then
height_earth>=0 %results in [1 1 1 1 1 0 0 0]
You have written while height_earth>=0. While needs an expression that will result in a single logical value, that is, result to true or false, but you are giving it an array. The code doesn’t enter the while loop and hence the value of L remains unchanged.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by