how to save values in variable from loop

1 回表示 (過去 30 日間)
siki
siki 2016 年 12 月 22 日
編集済み: Massimo Zanetti 2016 年 12 月 22 日
for a=1:10
fprintf('%d\n',a);
end
the above code will generate numbers from 1 to 10 and if I call variable 'a' from workspace it will display only last number i.e. 10. But I want to save all values from 1 to 10 in a matrix. For example, if I call variable 'a' result should be something like this [1 2 3 4 5 6 7 8 9 10] or in transpose form rather only last value (10).

回答 (1 件)

Massimo Zanetti
Massimo Zanetti 2016 年 12 月 22 日
編集済み: Massimo Zanetti 2016 年 12 月 22 日
Save vector a before printing:
a = 1:10;
for k=a
fprintf('%d\n',k);
end
a
Outputs:
1
2
....
10
a =
1 2 3 4 5 6 7 8 9 10

カテゴリ

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