OKPEDIA MATLAB EN VETTORI

Adding a Scalar to a Vector in MATLAB and Octave

In MATLAB and Octave, adding a scalar to a vector is straightforward. Utilize the addition operator (+):

v+n

In this context, `v` denotes the vector, while `n` stands as the scalar.

Executing this operation 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} $$

Every element in the vector is incremented by the scalar n).

This operation, by its nature, respects the commutative property: $$ \vec{v} + n = n + \vec{v} $$

Note.The procedure of adding a scalar to a vector is termed "scalar addition", distinctly different from the conventional vector addition.

Examples

Example 1

Consider the vector:

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

Adding a scalar, say 1:

>> v+1
ans =
2
3
4

This operation in MATLAB/Octave produces:

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

When adding the scalar 1 to the vector v:

>> 1+v
ans =
2
3
4

The result is consistent, affirming 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