フィルターのクリア

How to Save the outputs of my for loop?

1 回表示 (過去 30 日間)
cestcandice
cestcandice 2017 年 4 月 19 日
コメント済み: cestcandice 2017 年 4 月 19 日
I have this code:
x=1:10;
for i=1:length(x);
y=rand(i)
end
which is doing what I want to, but it's not saving the answers in my workspace, so I tried this:
x=1:10;
for i=1:length(x);
y(i)=rand(i)
end
But that comes out with an error that says, "In an assignment A(:) = B, the number of elements in A and B must be the same."
Thanks for the help..

採用された回答

James Tursa
James Tursa 2017 年 4 月 19 日
編集済み: James Tursa 2017 年 4 月 19 日
rand(i) is going to create a different sized matrix for each iteration, namely a square matrix of size i x i. That's why you are getting the error. If this is really what you want, then you can use cell arrays. E.g.,
y{i} = rand(i);
If you are just trying to generate a single random number for each iteration, then
y(i) = rand;
Or in the latter case you could just get rid of the loop entirely with:
y = rand(size(x));
  1 件のコメント
cestcandice
cestcandice 2017 年 4 月 19 日
You're brilliant! Thank you. :)

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

その他の回答 (0 件)

カテゴリ

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