Matlab

How to Use Dot Asterisk Operator in MATLAB

Element-wise multiplication operation is one of the useful operations that can be used for a variety of data analysis tasks, such as calculating the dot product of two vectors and multiplying a vector by scalar or matrix by vector. MATLAB makes it easy for the users by introducing the dot asterisk operator that is used especially for this purpose.

Read this guide to learn how to use the dot asterisk operator in MATLAB.

What is a dot asterisk Operator?

The dot asterisk operator denoted as (.*) is widely used in MATLAB for performing element-wise multiplication operations. The element-wise multiplication operation can be performed between two vectors, matrices, or arrays under the condition that both vectors, matrices, and arrays must be of the same size.

This operator is useful in MATLAB for several cases, some of the examples of this operator are:

Example 1: How to Multiply Two Vectors in MATLAB Using the (.*) Operator?

Consider an example to understand how to multiply vectors in MATLAB using the (.*) operator. In this example, we define a column vector a of size 10-by-1 and a column vector b of size 10-by-1. After that, we perform element-wise multiplication on a and b and obtain a vector c of the size 10-by-1.

a = [1:10]';
b = [2:2:20]'
;
c = a.*b

 

Example 2: How to Multiply Matrices in MATLAB Using the .* Operator?

The given example defines two matrices A and B having the same size 3-by-4. After that, it performs element-wise multiplication on them using the (.*) operator and obtains a matrix C of the size 3–by-4.

A = rand(3,4);
B = randn(3,4);
C = A.*B

 

Example 3: How to Multiply Arrays in MATLAB Using the .* Operator?

This MATLAB code creates two arrays A and B having the same size 3-by-4-by-2. After that, it performs element-wise multiplication on them using the (.*) operator and obtains an array C of the size 3–by-4-2.

A = rand(3,4,2);
B = randn(3,4,2);
C = A.*B

 

Conclusion

MATLAB is a useful tool that was initially designed for performing array operations. Element-wise array multiplication is an operation that allows us to multiply the element of the first array by the corresponding element of the second array using the (.*) operator. To perform this operation both arrays must be of the same size. This guide has covered multiple examples of how to use the (.*) operator to perform element-wise array multiplication in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.