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 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!