OKPEDIA MATLAB EN VETTORI

Difference between a vector and a scalar in Matlab and Octave

To subtract a vector and a scalar number on Matlab / octave use the subtraction operator (-)

v+n

The term v is a vector. The term n is a scalar number.

This operation outputs another vector

$$ \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} $$

The operation subtracts the scalar number n from each element of the vector.

Note. The subtraction between a vector and a scalar is called a scalar subtraction. This is a different operation than vector subtraction.

Examples

Example 1

Define a vector

>> v=[1;2;3]
v =
1
2
3

Calculate the difference between the vector and the scalar number 1

>> v-1
ans =
0
1
2

The result is a new vector.

$$ \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

Calculate the difference between the scalar number 1 and the vector v

>> 1-v
ans =
0
-1
-2

The result is another vector

$$ 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} $$

Scalar subtraction is not a commutative operation.

https://how.okpedia.org/en/matlab/difference-between-a-vector-and-a-scalar-in-matlab-and-octave


Report us an error or send a suggestion to improve this page


Arrays and Vectors


FacebookTwitterLinkedinLinkedin