MATLAB - Array Division



MATLAB is a powerful programming language widely used for numerical computing, data analysis, and visualization. One of its fundamental operations is array division, which allows you to perform element-wise division on arrays and matrices.

Array Division

In MATLAB, array division is performed using the element-wise division operator (./). This operator allows you to divide corresponding elements of two arrays or matrices of the same size, creating a new array with the division results.

Syntax

result = array1 ./ array2;

Let us check the division of vectors and arrays in the examples below.

Division of Two Vectors

In this example, each element of vector1 is divided by the corresponding element of vector2, resulting in the array [5, 5, 5].

Example

vector1 = [10, 20, 30]
vector2 = [2, 4, 6]
result = vector1 ./ vector2

When you execute the same in matlab command window the output is −

vector1 =

    10    20    30

vector2 =

     2     4     6

result =

     5     5     5

Division of Arrays

In the example we have matrix1 and matrix2. The operator (./) is used to divide matrix1 with matrix2. The result holds the output.

Example

matrix1 = [1, 2; 3, 4]
matrix2 = [0.5, 1; 1.5, 2]
result = matrix1 ./ matrix2

When you check the output in matlab command window the output is −

matrix1 =

     1     2
     3     4

matrix2 =

    0.5000    1.0000
    1.5000    2.0000

result =

     2     2
     2     2

rdivide() Method in Matlab for Division

MATLAB, a widely used programming language for numerical computation and data analysis, offers various built-in functions to simplify complex operations. One such function is rdivide(), performs element-wise division on arrays and matrices. In this article, we'll explore the rdivide() function, understand its functionality and go through the examples to understand how to make use of rdivide() on arrays and matrices.

The rdivide() function in MATLAB performs element-wise division of two arrays or matrices, similarly to the ./ operator. However, rdivide() simplifies the syntax and provides a more intuitive way to achieve the same result.

Syntax

result = rdivide(array1, array2);

Here are a few points that I came across that have advantages for using the rdivide() method.

  • The syntax is simple and the use of the rdivide() function eliminates the need to explicitly use the ./ operator for element-wise division, making code more readable.
  • The function's name conveys the division operation clearly, improving the clarity of your code.

Here are a few examples that you can try at your end.

Using rdivide() with Vectors

In the example below we have two vectors: vector1 with [10, 20, 30] and vector2 with [2,4,6]. The rdivide() method is used to divide vector1 with vector2.

Example

vector1 = [10, 20, 30]
vector2 = [2, 4, 6]
result = rdivide(vector1, vector2)

When you execute the above code in matlab command window the output is −

vector1 =

    10    20    30

vector2 =

     2     4     6

result =

     5     5     5

Using rdivide() on Matrices

In the example below we have taken two matrices matrix1 and matrix2. The rdivide() function is used to divide matrix1 with matrix2.

Example

matrix1 = [1, 2; 3, 4]
matrix2 = [0.5, 1; 1.5, 2]
result = rdivide(matrix1, matrix2)

When the above code is executed in matlab command window the output is −

matrix1 =

     1     2
     3     4

matrix2 =

    0.5000    1.0000
    1.5000    2.0000

result =

     2     2
     2     2

Right Matrix Division Using mrdivide() and / Operator

The division operation can be performed using the / operator and the mrdivide() function. Here will understand the working of right matrix division and how to use the / operator and the mrdivide() function and also see a few examples on it.

Right Matrix Division with / Operator

Syntax

X = A/B

X = A/B performs right-matrix division.

Here A is Numerator and it can be scalar, vector or matrix.

B is a denominator and it can be a scalar , vector or matrix.

Let us see few examples.

Example 1

In the example below we are going to divide matrix A with matrix B.

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

On execution the output is −

A =

     7     8
     5     6

B =

     3     2
     1     4

X =

    2.0000    1.0000
    1.4000    0.8000

Example 2

Let us divide the matrix with a scalar value as shown below.

A = [7, 8; 5, 6]
B = 2
X = A/B

When you execute the same in matlab command window the output is −

A =

     7     8
     5     6

B = 2

X =

    3.5000    4.0000
    2.5000    3.0000

Using mrdivide() Method

The mrdivide() function is an alternative way to perform right matrix division. It allows you to explicitly use the division syntax and is especially useful when dealing with complex equations involving matrices.

Syntax

X = mrdivide(A, B)

The above syntax is the same as A/B.

Let us test few examples using mrdivide()

Example

In the example below we have two matrices A1 and B1. The output of A1/B1 is done using mrdivide() method.

A1 = [2, 3, 1; 0, 8, 4; 1, 1, 0]
B1 = [7, 6, 6; 1, 0, 5; 9, 0, 4]
X = mrdivide(A1,B1)

When you execute the same in matlab command window the output is −

A1 =

     2     3     1
     0     8     4
     1     1     0


B1 =

     7     6     6
     1     0     5
     9     0     4


X =

    0.5000   -0.2927   -0.1341
    1.3333    0.0325   -1.0407
    0.1667   -0.2033    0.0041 
Advertisements