Script using loop and if statement

1 回表示 (過去 30 日間)
Juan Zegarra
Juan Zegarra 2019 年 4 月 28 日
編集済み: Raj 2019 年 4 月 29 日
Hello, can you please help me with this problem?
*Write a script using a loop that will take a vector (of any size) of numbers and remove all numbers that are between 5.0 and 9.0 and assign the remaining vector to a new variable.
Example: A=[1,5,8,10,13,18,20,9,4,6,8] B=[1,10,13,18,20,4]
B=A(find(A<5 | A>9))
if A<5 |A>9
would that be correct to use?
Please I really need help with this one.
Thank you
  1 件のコメント
dpb
dpb 2019 年 4 月 28 日
Doesn't meet the lesson requirement of using a loop...

サインインしてコメントする。

回答 (1 件)

Raj
Raj 2019 年 4 月 29 日
編集済み: Raj 2019 年 4 月 29 日
There are many ways to do this. Since your problem specifically asks for use of 'loops', you can use something like this:
A = input('enter A:') % say [1,5,8,10,13,18,20,9,4,6,8]
for i=1:length(A)
if A(i)<5 | A(i)>9
B(i)=A(i);
else
end
end
B=nonzeros(B)' % it will give B=[1,10,13,18,20,4]

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by