I am currently trying to make a column array out of the different values that are produced during a while loop. A new value of n is computed each time the loop executes. Now how can I put all those values into a column array?

2 ビュー (過去 30 日間)
Sam Sims
Sam Sims 2017 年 10 月 5 日
編集済み: Stephen 2017 年 10 月 5 日
n = input('Integer');
i = 1;
while n ~= 1;
if mod(n,2) == 0;
n = n/2;
else
n = 3*n + 1;
end
i = i + 1;
end

回答 (1 件)

Stephen
Stephen 2017 年 10 月 5 日
編集済み: Stephen 2017 年 10 月 5 日
Like this:
tableName = zeros(1,1);
n = input('Integer');
i = 1;
while n ~= 1;
if mod(n,2) == 0;
n = n/2;
else
n = 3*n + 1;
end
tableName(i,1) = n;
i = i + 1;
end

カテゴリ

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