For Loop / Sum of Equation
2 ビュー (過去 30 日間)
古いコメントを表示
Hi I am trying to figure the best way to write the sum of an equation from j=1 to j=100 and have the answers show up in the workspace. Right now in the workspace I only get Xj = 100. But I want a 1x100 matrix in the workspace. This is probable really simple but not sure how to do it
Thanks
My code:
for j = 1:100;
xj=j
end
0 件のコメント
回答 (1 件)
madhan ravi
2018 年 10 月 19 日
編集済み: madhan ravi
2018 年 10 月 19 日
X = zeros(1,100) %preallocation for speed
for j = 1:100 %an example
X(j) = j; %saves X in each element to avoid overwriting
end
X
6 件のコメント
madhan ravi
2018 年 10 月 19 日
Maybe you want this?
for j = 1:100 %an example
X(j) = j.^2; %saves X in each element to avoid overwriting
end
cumsum(X)
madhan ravi
2018 年 10 月 19 日
編集済み: madhan ravi
2018 年 10 月 19 日
It’s possible try the following
syms j
for i=1:100
Xj_symsym(i) = symsum(j^2, j, 1, i);
end
Xj_symsym
Xj_symsym = double(Xj_symsym)
is this what you are looking for?
参考
カテゴリ
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!