While loop not working (beginner)

7 次查看(过去 30 天)
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 个评论
David
David 2022-12-9
Hi! Did you try to assingn 'L' with a default value (Like 1) from out of the loop?

请先登录,再进行评论。

采纳的回答

Vinayak Choyyan
Vinayak Choyyan 2022-12-12
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by