how can fill a table using a loop ?
25 ビュー (過去 30 日間)
古いコメントを表示
Mohamed ben hadj yahia
2019 年 5 月 20 日
回答済み: Mohamed ben hadj yahia
2019 年 5 月 20 日
hello I m having a problem filling a table
let's say I have a table
a c
1
4
10
11
...
n
I want c to be the mean of a starting from 1 to n
for example
c(1) = 1
c(2) =1+4 / 2
c(3) = 1+4+10 / 3
c(4)=1+4+10+11 / 4
... n
how do I fill c using a loop ?
thank you
0 件のコメント
採用された回答
Adam Danz
2019 年 5 月 20 日
編集済み: Adam Danz
2019 年 5 月 20 日
No-loop method
There's no need for a loop.
b = cumsum(a)./(1:length(a));
Loop method
If you must use a loop...
b = zeros(size(a));
for i = 1:length(a)
b(i) = sum(a(1:i))/i;
end
If you want that in a table,
t = table(a',b','VariableNames',{'a','b'});
0 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!