how to sum a vector without sum func

2 次查看(过去 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
James Tursa 2017-7-27
编辑:James Tursa 2017-7-27
Maybe use plus and minus functions? What have you done so far?

请先登录,再进行评论。

采纳的回答

Star Strider
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 个评论
yuval ohayon
yuval ohayon 2017-7-28
star its look like the right answer, the function ones(10,1} is a column vector?
Star Strider
Star Strider 2017-7-28
Yes.
The ‘ones(length(v),1)’ call forms a (10 x 1) column vector.

请先登录,再进行评论。

更多回答(2 个)

Jan
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;

Jan de Jong
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 个评论
yuval ohayon
yuval ohayon 2017-7-31
Im sorry im not aware to -cospi×[1:10] Explain me please
Jan de Jong
Jan de Jong 2017-7-31
It will give an alternating sequence to account for the sign change in the sum.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by