OKPEDIA MATLAB EN VETTORI

Vector Addition in MATLAB and Octave

To add vectors in MATLAB and Octave, utilize the `+` operator.

Given vectors `v` and `w`:

v + w

Example

Example 1

Define vector `v`:

>> v=[1;2;3]

Define another vector `w`:

>> w=[4;5;6]

Both `v` and `w` are column vectors consisting of three elements.

To compute their sum, simply use v + w

>> v+w
ans =
5
7
9

The resulting vector is the sum of `v` and `w`.

Example 2

Define a row vector `z`:

>> z=[1 2]
z = 1 2

Define a column vector `u`:

>> u=[3;4]
u =
3
4

To add vectors `z` and `u`, execute:

>> z+u

The output is:

ans =
4 5
5 6

Note. In this instance, the result is a matrix. This is because the addition involves a row vector and a column vector. Mathematically, the operation is represented as: $$ z+u = \begin{pmatrix} 1 & 2 \end{pmatrix} + \begin{pmatrix} 3 \\ 4 \end{pmatrix} = \begin{pmatrix} 1+3 & 2+3 \\ 1+4 & 2+4 \end{pmatrix} = \begin{pmatrix} 4 & 5 \\ 5 & 6 \end{pmatrix} $$

https://how.okpedia.org/en/matlab/how-to-add-vectors-in-matlab-and-octave


Report an error or share a suggestion to enhance this page


Arrays and Vectors


FacebookTwitterLinkedinLinkedin