It's important to note the differences between scalar and matrix operations. For example, there is special syntax for scalar multiplication versus matrix multiplication.
- [Instructor] One thing that can trip people up when they first get started in MATLAB are the differences in how MATLAB handles matrix multiplication versus standard multiplication. So let's take a quick look. Just to review when we're multiplying matrices, let's say we have two two by two matrices we want to multiply together. To find the result, we start with the top left element and to get that we take the first row of the first matrix and the first column of the second matrix and we multiply their first elements together and then add those to the second elements.
So here we would have minus three plus 54 giving us a result of 51. We continue this process for the first row and second column, second row and first column, and finally the second row and second column of the two matrices populating our resulting matrix. Now this is different than element by element multiplication where we would say take minus one times three to get negative three, six times nine is two to get negative twelve and so on.
Now MATLAB can do both of these so let's take a look at how. Let's go ahead and close the variable editor here. Give ourselves some more room to work. I'm going to clear the workspace with clear all and clear the screen. So let's create our first matrix like we had in the example. I'm going to call it M1 and let's set that equal to our matrix here. I'll set my brackets and my semicolon. And we had negative one and six for the first row and two and four for the second.
And let's create our second matrix, M2, and that was equal to three and minus two in the first row and nine and five in the second. Now when we want to do standard matrix multiplication we just use the asterisk sign like we would usually use in a calculator. M1 times M2 and if we look at our resulting matrix it agrees with our previous example. If on the other hand, we want to do element by element multiplication, we use the dot asterisk.
So we do M1 dot times M2. And this will multiply the two matrices element by element. This type of syntax holds true for division. If we wanted to divide the elements of our matrix, one by one, we can do dot divided by M2 and divide element by element. So keep these differences in mind as you multiply and divide with matrices and vectors.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models