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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!