MATLAB For Dummies
Book image
Explore Book Buy On Amazon

MATLAB does so many neat little math tricks. As with matrix multiplication in MATLAB, matrix division takes place at several different levels. Keep reading to explore division at each level.

Dividing a vector by a scalar

Dividing a vector by a scalar and producing a usable result is possible. For example, type m = [2, 4, 6] / 2 and press Enter. You see the following result:

m =
  1  2  3

Each of the entries is divided by the scalar value. Notice that this is right division. Using left division (m = [2, 4, 6] 2) would produce an unusable result; however, using m = 2 [2, 4, 6] would produce the same result as before. MATLAB would do its best to accommodate you with a result, just not one you could really use.

Dividing a matrix by a vector

When dividing a matrix by a vector, defining the sort of result you want to see is important. Most people want to perform an element-by-element division. In this case, you use the bsxfun() function with the @rdivide function name — @rdivide for right division. To see how this works, type n = bsxfun(@rdivide, [2, 4; 6, 8], [2, 4]) and press Enter. You see the following output:

n =
  1  1
  3  2

In this case, the element in column 1, row 1 is defined by 2 / 2. Likewise, the element in column 1, row 2 is defined by 6 / 2.

Dividing two matrices

When dividing two matrices, the dimensions of the two matrices must agree. For example, you can’t divide a 3 x 2 matrix by a 2 x 3 matrix — both matrices must be the same dimensions, such as 3 x 2. To see how this works, type o = [2, 4; 6, 8] / [1, 2; 3, 4] and press Enter. You see the following result:

o =
  2  0
  0  2

Performing left division of two matrices is also possible. To see the result of performing left division using the same matrices, type p = [2, 4; 6, 8] [1, 2; 3, 4] and press Enter. Here’s the result you see:

p =
 0.5000   0
   0 0.5000

It’s essential to remember that matrix division isn’t actually division as most people think of it. What you really do is multiply one matrix by the inverse of the other. For example, using the two matrices you see here, you can accomplish the same result of left division by typing q = [2, 4; 6, 8] * inv([1, 2; 3, 4]) and pressing Enter.

To perform right division, you simply change the inverted matrix by typing r = inv([2, 4; 6, 8]) * [1, 2; 3, 4] and pressing Enter. The inv() function always returns the inverse of the matrix that you provide as input, so you can use it to help you understand precisely how MATLAB is performing the task.

However, using the inv() function is computationally inefficient. To make your scripts run faster, dividing is always better. You can use the inv() function in many ways. For example, multiplying any matrix by its inverse, such as by typing s = [1, 2; 3, 4] * inv([1, 2; 3, 4]), yields the identity matrix.

What some people are actually looking for is element-by-element division. To accomplish this task, you must use the bsxfun() function. For example, to perform left division on the two preceding matrices, you type t = bsxfun(@ldivide, [2, 4; 6, 8], [1, 2; 3, 4]) and press Enter. The result in this case is

t =
 0.5000 0.5000
 0.5000 0.5000

Likewise, you can perform right division. To see how this works, type u = bsxfun(@rdivide, [2, 4; 6, 8], [1, 2; 3, 4]) and press Enter. You see the following output:

u =
  2  2
  2  2

About This Article

This article is from the book:

About the book authors:

John Paul Mueller is an author and technical editor with experience in application development, database management, machine learning, and deep learning. He has written hundreds of books and articles helping everyday people learn everything from networking to database management.

John Mueller has produced 114 books and more than 600 articles on topics ranging from functional programming techniques to working with Amazon Web Services (AWS). Luca Massaron, a Google Developer Expert (GDE),??interprets big data and transforms it into smart data through simple and effective data mining and machine learning techniques.

This article can be found in the category: