Problem finding sum of array using vectorization

Trying to find the sum of this array using vectorization, the value of the sum should be 0.74 but when I run it I am getting 'ans = -Inf'. Where am I going wrong?
x = sym(1) ./ (1:9)
1 - sum( +1./((x)-1) ./ x )

답변 (1개)

James Tursa
James Tursa 2021년 12월 15일

0 개 추천

The first element of x is 1, so that element produces 1/(1-1) = 1/0 = inf in the second line. You need to rewrite that second line.

댓글 수: 2

Thank you! I know I need to rewrite the second line, I'm just not sure how to as I am new to MATLAB. Would you please be able to explain how?
James Tursa
James Tursa 2021년 12월 15일
편집: James Tursa 2021년 12월 15일
You just need a vector that alternates +1, -1, +1, -1, etc. to use for the sign, right? Hint: Start with mod(1:9,2) and see if you can manipulate it into what you want.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2021년 12월 15일

편집:

2021년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by