フィルターのクリア

How do you divide 120 by x when x is (3,6] starting with x=0

3 ビュー (過去 30 日間)
Caroline F
Caroline F 2022 年 3 月 5 日
回答済み: Walter Roberson 2022 年 3 月 5 日
I am trying to do a while statement that divdes 120 by the numbers between (3,6] and only gives nonzero() answers, but so far my code only gives me the last value "20". Is there a way to fix the code so I get all three values, 30, 24, and 20?
x=0;
while (x>2) & (x<=5)
x=x+1;
Q2(x) = 120/x;
end
disp('Q2 =')
Q2 =
disp(nonzeros(Q2))
Unrecognized function or variable 'Q2'.

採用された回答

Walter Roberson
Walter Roberson 2022 年 3 月 5 日
x=0;
while (x>2) & (x<=5)
Your x starts out at 0 which immediately fails the x>2 test.
Consider
x=0;
while x<=6
if (x>3) & (x<=6)
Now imagine that you were keeping a counter of how many values you have found, and you used the counter to index the output variable...

その他の回答 (1 件)

David Hill
David Hill 2022 年 3 月 5 日
編集済み: David Hill 2022 年 3 月 5 日
Q2=120./(4:6)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by