How do I write a loop which creates a random number and adds the previous values
古いコメントを表示
This is how i try it
for i=1:23;
a(i) = randn(1);
a = a + a(i);
end
採用された回答
その他の回答 (1 件)
Ajay Kumar
2020 年 3 月 24 日
編集済み: Ajay Kumar
2020 年 3 月 24 日
res_sum = 0;
for i=1:23;
a(i) = randn(1);
res_sum = res_sum + a(i);
end
4 件のコメント
Jehona
2020 年 3 月 24 日
Ajay Kumar
2020 年 3 月 24 日
編集済み: Ajay Kumar
2020 年 3 月 24 日
res_sum = 0;
for i=1:23;
a(i) = randn(1);
res_sum(i+1) = res_sum(i) + a(i);
end
the sum will be 1x24 because the first value of sum is 0. You can however delete the first element
res_sum = res_sum(2:end);
Adam Danz
2020 年 3 月 24 日
FYI, you don't need a loop to do this. See the last line of my answer for a non-loop method.
Ajay Kumar
2020 年 3 月 24 日
Thanks Adam.
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!