Problem finding sum of array using vectorization
4 次查看(过去 30 天)
显示 更早的评论
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 )
0 个评论
回答(1 个)
James Tursa
2021-12-15
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!