MATLAB: Use ./ in x = ( (-1).^(n+1) ) ./ (2*n – 1)

element-wise powerMATLAB

I need help about my line of thought in the following exercise:
Create a vector x with the elements,
xn = (-1)n+1/(2n-1) % n+1 is the exponent of (-1)
and add up the elements of the version of this vector that has 100 elements.
The solution is:
n = 1:100;
x = ( (-1).^(n+1) ) ./ (2*n - 1);
y = sum(x)
I need help to see if I'm understanding the solution itself 'cause I'm not sure about the use of .^ and ./ instead of ^ and /.
My interpretation is:
Considering the first 4 elements, for ex, we can think of it as two vectors, the numerator vector A and the denominator vector B:
A = [A1 A2 A3 A4] = [1 -1 1 -1]
B = [B1 B2 B3 B4] = [1 3 5 7]
Since I want to establish the relationship between A and B such as [A1/B1 A2/B2 …] I must work with element wise operations such as ./.
About .^ usage, I cannot use ^ because I'm not working with multidimensional arrays (matrices) but with unidimensional arrays (vectors).
Am I thinking correctly?

Best Answer

Yes, you need to use .^ and ./ instead of ^ and / because at least one of the operands is not a scalar. If at least one of the operands is not a scalar, then ^ and / would be matrix algebra operations instead of element-wise operations (which is not what you want in this case).
The vector vs 2D (or nD) matrix thing really does not matter here ... both would cause matrix algebra operations to take place for ^ and / (rather than element-size for .^ and ./)