Skip to main content
St Louis

Back to all posts

How to Perform Element-Wise Operations on Matrices In MATLAB?

Published on
5 min read
How to Perform Element-Wise Operations on Matrices In MATLAB? image

Best MATLAB Tools to Buy in October 2025

1 MATLAB For Dummies (For Dummies (Computer/Tech))

MATLAB For Dummies (For Dummies (Computer/Tech))

BUY & SAVE
$23.60 $34.99
Save 33%
MATLAB For Dummies (For Dummies (Computer/Tech))
2 MATLAB: A Practical Introduction to Programming and Problem Solving

MATLAB: A Practical Introduction to Programming and Problem Solving

BUY & SAVE
$48.80 $66.95
Save 27%
MATLAB: A Practical Introduction to Programming and Problem Solving
3 MATLAB and Simulink Crash Course for Engineers

MATLAB and Simulink Crash Course for Engineers

BUY & SAVE
$44.99 $59.99
Save 25%
MATLAB and Simulink Crash Course for Engineers
4 An Introduction to MATLAB® Programming and Numerical Methods for Engineers

An Introduction to MATLAB® Programming and Numerical Methods for Engineers

  • LEARN MATLAB WITH STEP-BY-STEP PROGRAMMING INSTRUCTIONS.
  • HANDS-ON EXERCISES REINFORCE KEY PROGRAMMING CONCEPTS.
  • IDEAL FOR BEGINNERS LOOKING TO MASTER MATLAB EFFECTIVELY.
BUY & SAVE
$38.86 $79.95
Save 51%
An Introduction to MATLAB® Programming and Numerical Methods for Engineers
5 MATLAB: A Practical Introduction to Programming and Problem Solving

MATLAB: A Practical Introduction to Programming and Problem Solving

BUY & SAVE
$30.67 $64.95
Save 53%
MATLAB: A Practical Introduction to Programming and Problem Solving
6 MATLAB Programming for Engineers

MATLAB Programming for Engineers

BUY & SAVE
$78.44 $140.95
Save 44%
MATLAB Programming for Engineers
+
ONE MORE?

Element-wise operations in MATLAB allow you to perform operations on corresponding elements of two matrices. To perform element-wise operations on matrices in MATLAB, you can use the dot operator (.) or use built-in functions specifically designed for element-wise operations.

For example, let's consider two matrices A and B of the same size:

A = [1, 2, 3; 4, 5, 6]; B = [2, 3, 4; 5, 6, 7];

  1. Addition: You can add corresponding elements of matrices A and B using the addition operator (+) with the dot operator (.):

C = A + B;

  1. Subtraction: Similarly, you can subtract corresponding elements using the subtraction operator (-) with the dot operator:

C = A - B;

  1. Multiplication: For element-wise multiplication, you can use the multiplication operator (*) with the dot operator:

C = A .* B;

Note that element-wise multiplication is different from matrix multiplication in MATLAB.

  1. Division: Element-wise division can be performed using the division operator (/) with the dot operator:

C = A ./ B;

  1. Exponentiation: To raise each element of a matrix to a power, you can use the element-wise exponentiation operator (.^):

C = A .^ 2;

In addition to these basic element-wise operations, MATLAB provides various built-in functions that operate element-wise on matrices. Some commonly used functions include sin, cos, log, and sqrt. To perform a specific operation, you can simply apply the function to the matrix you want to operate on:

C = sin(A);

These are just a few examples of how to perform element-wise operations on matrices in MATLAB. Element-wise operations are a fundamental concept in MATLAB and are useful for many calculations involving matrices and arrays.

How to perform element-wise comparison of matrices in MATLAB?

To perform element-wise comparison of matrices in MATLAB, you can use the relational operators like ==, >, <, >=, <=, ~=.

Here is an example:

% Define two matrices A = [1 2 3; 4 5 6; 7 8 9]; B = [6 5 4; 3 2 1; 9 8 7];

% Perform element-wise comparison C = A == B; % Equality comparison D = A > B; % Greater than comparison E = A < B; % Less than comparison F = A >= B; % Greater than or equal to comparison G = A <= B; % Less than or equal to comparison H = A ~= B; % Not equal to comparison

% Display the results disp(C); disp(D); disp(E); disp(F); disp(G); disp(H);

Output:

 0     0     0
 0     0     0
 0     0     0

 0     0     0
 1     1     1
 0     0     0

 1     1     1
 0     0     0
 1     1     1

 0     0     0
 1     1     1
 1     1     1

 1     1     1
 0     0     0
 0     0     0

 1     1     1
 0     0     0
 1     1     1

In the example above, we perform element-wise comparisons between matrices A and B, and store the results in matrices C, D, E, F, G, and H. Each element of the result matrices is true if the corresponding elements of A and B satisfy the given condition, and false otherwise.

How to calculate the exponential of each element in a matrix using MATLAB?

To calculate the exponential of each element in a matrix using MATLAB, you can use the exp() function in MATLAB. Here's the step-by-step process:

  1. Define or create your matrix. For example, let's say you have a 2x3 matrix called A: A = [1 2 3; 4 5 6];
  2. Use the exp() function to calculate the exponential of each element in the matrix. Assign the result to a new matrix, let's call it B: B = exp(A); The exp() function will element-wise apply the exponential function to each element in the matrix A and store the results in the matrix B.
  3. Display the resulting matrix B: disp(B); This will display the exponential of each element in matrix A.

Here's the complete code:

A = [1 2 3; 4 5 6]; B = exp(A); disp(B);

Output: 2.7183 7.3891 20.0855 54.5982 148.4132 403.4288

This will display the exponential of each element in the matrix A as shown above.

What is the square root of each element in a matrix using MATLAB?

To find the square root of each element in a matrix using MATLAB, you can use the element-wise square root operator '.':

matrix = [1, 4, 9; 16, 25, 36]; % example matrix

sqrt_matrix = sqrt(matrix)

% Output: % sqrt_matrix = [1, 2, 3; 4, 5, 6]

In this example, the sqrt() function is applied to each element of the matrix, resulting in a new matrix with the square root of each element.

How to divide matrices element-wise in MATLAB?

To divide matrices element-wise in MATLAB, you can use the ./ operator. Here's how:

  1. Create or define two matrices, A and B, that you want to divide element-wise.
  2. Use the ./ operator to perform element-wise division of the matrices.

Here's an example:

A = [1 2 3; 4 5 6; 7 8 9]; % A is a 3x3 matrix B = [2 4 6; 8 10 12; 14 16 18]; % B is a 3x3 matrix

C = A ./ B; % Element-wise division of A and B

disp(C);

Output:

0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000

In this example, each element of matrix A is divided by the corresponding element of matrix B using the ./ operator. The result is stored in the matrix C, and then printed using the disp() function.