OKPEDIA MATLAB EN VETTORI

How to add a scalar to a vector in Matlab and Octave

To add a vector and a scalar number in Matlab / octave we use the addition 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} $$

Each element of the vector is added to the scalar number n.

The operation of adding a vector and a scalar number satisfies the commutative property $$ \vec{v} + n = n + \vec{v} $$

Note. The addition between a vector and a scalar is called scalar addition. It is a different operation than vector addition.

Examples

Example 1

Define a vector

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

Add the vector and the scalar number 1

>> v+1
ans =
2
3
4

In Matlab/Octave the result is a new vector.

Each element of the vector is the sum of the element and the scalar number

$$ \vec{v} + 1 = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} + 1 = \begin{pmatrix} 1 + 1 \\ 2 + 1 \\ 3 + 1 \end{pmatrix} = \begin{pmatrix} 2 \\ 3 \\ 4 \end{pmatrix} $$

Example 2

Add the scalar number 1 with the vector v

>> 1+v
ans =
2
3
4

The result is the same because the addition satisfies the commutative property

$$ 1 + \vec{v} = 1 + \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} + 1 = \begin{pmatrix} 1 + 1 \\ 1 + 2 \\ 1 + 3 \end{pmatrix} = \begin{pmatrix} 2 \\ 3 \\ 4 \end{pmatrix} $$

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


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


Arrays and Vectors


FacebookTwitterLinkedinLinkedin