フィルターのクリア

While loop not working (beginner)

8 ビュー (過去 30 日間)
Alexis
Alexis 2022 年 12 月 9 日
回答済み: Vinayak Choyyan 2022 年 12 月 12 日
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 Center および File ExchangeLoops 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