Subtraction of a Vector and a Scalar in MATLAB and Octave
In MATLAB and Octave, to subtract a scalar from a vector, simply use the subtraction operator (-).
v+n
Given:
- v represents a vector.
- n denotes a scalar.
The expression ( v - n) yields:
$$ \vec{v} - n = \begin{pmatrix} a_1 \\ a_2 \\ a_3 \end{pmatrix} - n = \begin{pmatrix} a_1 - n \\ a_2 - n \\ a_3 - n \end{pmatrix} $$
This operation subtracts the scalar "n" from each component of the vector "v".
Note. The subtraction of a scalar from a vector is termed "scalar subtraction." It's crucial to distinguish this from vector subtraction.
Examples
Example 1
Define a vector
>> v=[1;2;3]
v =
1
2
3
Subtract the scalar 1 from vector v
>> v-1
ans =
0
1
2
This results in:
$$ \vec{v} - 1 = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} - 1 = \begin{pmatrix} 1 - 1 \\ 2 - 1 \\ 3 - 1 \end{pmatrix} = \begin{pmatrix} 0 \\ 1 \\ 2 \end{pmatrix} $$
Example 2
Subtract vector v from the scalar 1
>> 1-v
ans =
0
-1
-2
This yields:
$$ 1 - \vec{v} = 1 - \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} = \begin{pmatrix} 1 - 1 \\ 1 - 2 \\ 1 - 3 \end{pmatrix} = \begin{pmatrix} 0 \\ -1 \\ -2 \end{pmatrix} $$
It's important to note that scalar subtraction is not commutative.