OKPEDIA MATLAB EN VETTORI

How to calculate element-wise multiplication of vectors in Matlab and Octave

To calculate the element-wise product of two vectors in Matlab / Octave use the operator. *

v.*w

The terms v and w are two vectors of the same size.

The result is a vector where each element i is the product of elements i of the original two vectors

$$ \vec{v} \cdot \vec{w} = \begin{pmatrix} v_1 \\ v_2 \\ v_3 \\ \vdots \\ v_n \end{pmatrix} \cdot \begin{pmatrix} w_1 \\ w_2 \\ w_3 \\ \vdots \\ w_n \end{pmatrix} = \begin{pmatrix} v_1 \cdot w_1 \\ v_2 \cdot w_2 \\ v_3 \cdot w_3 \\ \vdots \\ v_n \cdot w_n \end{pmatrix} $$

Note. This vector multiplication is also known as Hadamard product, Schur product or element-wise product.

Example

Define a vector v

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

Define another vector w with the same number of elements

>> w=[4;5;6]
w =
4
5
6

Compute the element-wise product of the two vectors

>> v.*w
ans =
4
10
18

Matlab calculates the component-by-component product of the elements.

The result is a vector composed of the products of the elements of the two vectors that are in the same position

$$ \vec{v} \cdot \vec{w} = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} \cdot \begin{pmatrix} 4 \\ 5 \\ 6 \end{pmatrix} = \begin{pmatrix} 1 \cdot 4 \\ 2 \cdot 5 \\ 3 \cdot 6 \end{pmatrix} = \begin{pmatrix} 4 \\ 10 \\ 18 \end{pmatrix} $$

https://how.okpedia.org/en/matlab/how-to-calculate-element-wise-multiplication-of-vectors-in-matlab-and-octave


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


Arrays and Vectors


FacebookTwitterLinkedinLinkedin