How to raise vector elements to a power in Matlab and Octave
To raise the elements of a vector to a power, use the operator .^
v.^n
Term v is a vector, term n is an integer.
The result is a vector in which each element is raised to the nth power
$$ \frac{ \vec{v} }{ \vec{w} } = \begin{pmatrix} v_1 \\ v_2 \\ v_3 \\ \vdots \\ v_n \end{pmatrix} \ : \ \begin{pmatrix} w_1 \\ w_2 \\ w_3 \\ \vdots \\ w_n \end{pmatrix} = \begin{pmatrix} \frac{ v_1 }{ w_1 } \\ \frac{ v_2 }{ w_2 } \\ \frac{ v_3 }{ w_3 } \\ \vdots \\ \frac{ v_n }{ w_n } \end{pmatrix} $$
Note. This power is also known as element-wise power.
Example
Example 1
Create a vector v
>> v=[2;3;4]
v =
2
3
4
Square each element of array.
>> v.^2
ans =
4
9
16
The result is a vector composed of the squares of the elements
$$ [ \vec{v} ]^2 = \begin{pmatrix} 2 \\ 3 \\ 4 \end{pmatrix}^2 = \begin{pmatrix} 2^2 \\ 3^2 \\ 4^2 \end{pmatrix} = \begin{pmatrix} 4 \\ 9 \\ 16 \end{pmatrix} $$
Example 2
Calculate the power n = -1 of the elements of the vector.
>> v.^(-1)
ans =
0.50000
0.33333
0.25000
The result is
$$ [ \vec{v} ]^{-1} = \begin{pmatrix} 2 \\ 3 \\ 4 \end{pmatrix}^{-1}= \begin{pmatrix} 2^{-1} \\ 3^{-1} \\ 4^{-1} \end{pmatrix} = \begin{pmatrix} \frac{1}{2} \\ \frac{1}{3} \\ \frac{1}{4} \end{pmatrix} = \begin{pmatrix} 0.50000 \\ 0.33333 \\ 0.25000 \end{pmatrix} $$