need help on a simple code, for, while

function [A,B,C]=task5(x)
for k=1:x
A=(1:k);
end
while (x~=0 & x >0 )
x=(1:x);
B=x;
end
C=(1:x);
output A,B,C shoud be the same, and answer is from 1 to input number. for example, if input is 3 ,then answer is 1,2,3. something is wrong with my code, need help... thanks

 採用された回答

Ridwan Alam
Ridwan Alam 2019 年 11 月 21 日
編集済み: Ridwan Alam 2019 年 11 月 21 日

0 投票

Just to be consistent with your question:
function [A,B,C]=task5(x)
A = [];
for k=1:x
A=[A,k];
end
y = x;
B = [];
while (y>0)
B=[B,x-y+1];
y = y-1;
end
C=[1:x];

3 件のコメント

leeee
leeee 2019 年 11 月 21 日
need use for and while, not if, thanks
Ridwan Alam
Ridwan Alam 2019 年 11 月 21 日
編集済み: Ridwan Alam 2019 年 11 月 21 日
Sure. Using loop reduce the need for range operator (1:x).
Also, please run the code and see if the results match, before rejecting the answer.
leeee
leeee 2019 年 11 月 21 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 11 月 21 日

コメント済み:

2019 年 11 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by