This tutorial will introduce the
.* operator, which is used for element-by-element multiplication in MATLAB.
Element-By-Element Multiplication Using the
.* Operator in
.* operator is used for the element-by-element multiplication of two vectors or matrixes in MATLAB. The dimensions of the two vectors or matrixes must be the same for multiplication; otherwise, there will be an error. Element-by-element means the first element of one vector will be multiplied by the first element of the other vector, and the second element will be multiplied with the second element, and so on. For example, if you want to multiply two vectors of the same number of elements, you can use it. See the code below.
a = [1 2 3] b = [3 2 1] c = a.*b
c = 3 4 3
In the above code, we perform element-by-element multiplication of two vectors of the same length and save the result in another variable. As you can see from the output, the first element of one vector is multiplied by the first element of the other vector, and so on. You can also multiply two column vectors of the same size. Now, what if we multiply a row matrix with a column matrix using the
.* operator. See the code below.
a = [1 1 1] b = [1;2;3;4] c = a.*b
c = 1 1 1 2 2 2 3 3 3 4 4 4
In the above code, we multiply a row vector
a with a column vector
b, and save the result in variable
c. As you can see, the first element of column vector
b is multiplied by the entire row vector
a and so on.