Join Patrick Royal for an in-depth discussion in this video Working with matrix and scalar operations, part of Learning MATLAB.
Let's look at the basic matrix operations in use by MATLAB. Since MATLAB treats every variable as if it were a matrix, it's important to distinguish between operations like matrix multiplication versus scaler multiplication. To practice different types of matrix operations, I'll start by running the matrix_generator.m script in Exercise files. This script simply creates a few sample matrices that we can then manipulate as we go through this video. To start with, type in a plus b in the command window. Since matrix a and matrix b have the same dimension, MATLAB will interpret this as piecewise addition and will simply add the corresponding values in each cell of the matrices.
On the other hand, if you type in a plus 2, MATLAB will interpret this as scalar addition, so it will add two to every cell in A. The same principle works for multiplying matrices. If we type in c times d, MATLAB sees that both of these variables are matrices, and their inner dimensions match. So it will automatically carry out matrix multiplication, on the other hand if you type in c times 2 MATLAB understands 2 as a constant and will multiply every term in c by 2.
Matrix multiplication is a fairly straight forward process. But what if we wanted MATLAB to do a piece wise multiplication of corresponding entries of the matrix instead. Matrices a and b have the same dimensions but if you just input a times b, MATLAB doesn't know we want to multiply corresponding entries. So it assumes we are indicating matrix multiplication and throws an error because the inner dimensions of these two matrices don't match. Instead we can add a dot in front of the asterisk.
Adding a dot in front of any operation, addition, subtraction, multiplication, division, exponents, or even equality, tells MATLAB to execute the operation piecewise. This is a really efficent way to manipulate large amounts of data at once without four loops or recursion. Now when I run the command it will generate for me a new two by three matrix where each entry is a product of cooresponding entries of matrices a and b.
Putting a dot in front of an operation should not be confused with taking a dot product dot. if you use vectors E and F, and type in e dot times f, that will return a new vector where each entry is a product of corresponding entries of the matrices.. To do a dot product. You have to transpose the second matrix. And then you use simple matrix multiplication. So, in this example, we would input e times f, apostrophe. Where the apostrophe tells MATLAB to transpose f before it does the calculation.
In general, any time you're working with 1 by 1 matrix, MATLAB will treat them as scalers. Any time you are working with larger matrices, MATLAB will treat them as matrices. If you want MATLAB to perform an operation piecewise, place a dot in front of the operation symbol. If you want to take a dot product, just treat the vectors like they were matrices, and then just use matrix multiplication.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox