how to sum a vector without sum func
1 回表示 (過去 30 日間)
古いコメントを表示
given this elements:19^3 −17^3 +15^3 −13^3 +11^3 −9^3 +7^3 −5^3 +3^3 −1^3 notice the sign changing 9 times with 10 element again without sum function\ anyone idea?
1 件のコメント
James Tursa
2017 年 7 月 27 日
編集済み: James Tursa
2017 年 7 月 27 日
Maybe use plus and minus functions? What have you done so far?
採用された回答
Star Strider
2017 年 7 月 28 日
The elements are cubed, so the signs are conserved.
This works:
v = [19^3 -17^3 +15^3 -13^3 +11^3 -9^3 +7^3 -5^3 +3^3 -1^3];
sum_v = v*ones(numel(v),1)
Check = sum(v) % Check
sum_v =
3970
Check =
3970
2 件のコメント
その他の回答 (2 件)
Jan
2017 年 7 月 28 日
What about:
v = 19^3 - 17^3 + 15^3 - 13^3 + 11^3 - 9^3 + 7^3 - 5^3 + 3^3 - 1^3;
0 件のコメント
Jan de Jong
2017 年 7 月 31 日
編集済み: Walter Roberson
2017 年 7 月 31 日
Or a little more general:
val = [19:-2:1]'; sig = -cos(pi*[1:10]);
s = sig*val.^3;
2 件のコメント
Jan de Jong
2017 年 7 月 31 日
It will give an alternating sequence to account for the sign change in the sum.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!