While loop not working (beginner)
7 views (last 30 days)
Show older comments
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 Comment
David
on 9 Dec 2022
Hi! Did you try to assingn 'L' with a default value (Like 1) from out of the loop?
Accepted Answer
Vinayak Choyyan
on 12 Dec 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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!