Matrix operations take on many forms, some of which require special matrices that contain all zeros, all ones, or give you the same matrix back through multiplication. In this video, learn how to create several types of special matrices.
- [Instructor] Matrix operations take on many forms, some of which require special matrices that contain all zeroes, all ones, or give you the same matrix back through multiplication. In this movie I will show you how to create several types of special matrices. I'm in MATLAB and I have a blank command window. The first type of special matrix that I will show you how to create is a zeroes matrix. And as the name implies, this is the matrix that contains zeroes for each of its values.
You can use these matrices as initialization points for example, neural network nodes. You can assign a weight to each one. To create a zeroes matrix that is square you only need to pass one argument and that is the number of rows and columns. The keyword or function is zeroes, then a left parenthesis and the number of rows and columns. So if you wanted to do a four by four, you just type a four within parenthesis and enter, and you get a four by four.
Now note that I am not assigning this to any variables, I'm just using answer. If you want to create a two by three then you can do zeroes left parenthesis and then two comma three, so that would be two rows, three columns. Right parenthesis enter and there you go. You can do the same thing with the number one. The keyword or function for that is ones and it's exactly the same as zeroes. So if you wanted to do four by four with ones, just type in a four between parenthesis and enter and you get a four by four, or you could do ones and we'll do a three by two this time, so ones and then three comma two within parenthesis, enter and you get a three row, two column matrix where every value is one.
You can also put in random values, these could be randomized starting weights for nodes in a neural network. If you want to do random values between zero and one, that is real numbers, decimal numbers, then you can assign that to a variable. So I'll do mat rand which I am using to mean random matrix, although it's not a keyword it's just a variable name, then equal and I will assign it rand. Then a left parenthesis and then two four.
So I'm making a two by four matrix, two rows, four columns made up of random values from zero to one. Enter, and there they are. If you want to do random integers, then you can provide the range. So I'll do mat rand I equals and those will be the values rand I which is the random integer function, followed by a left parenthesis and the range for the values will be between seven comma 17, so the maximum value would be 17, the minimum will be seven then a comma, and I want it to be five rows by four columns.
So what I have between parenthesis are in square brackets seven comma 17 then a comma, number five which was the number of rows and four which is the number of columns. And when I press enter I get the matrix that I was looking for. I can pass one argument to make it a square matrix. So if I did mat rand two equals, and I'll do rand three so I should get a three by three matrix of random real values between zero and one, and there I have it.
You could also create the multiplicative identity or identity matrix. If you multiply a matrix of the proper size by the identity matrix then you will get the same matrix back. So let's say that I want to multiply mat rand two a three by three matrix by an identity matrix. The identity matrix for a three by three would be eye, E Y E three and enter. And you see that you have a one going down the main diagonal, so you start at the top left position one one, then two two and three three.
So that's the identity matrix. And if I multiply mat rand two by eye three and enter, I get exactly the same values back. Now note that even though it is tempting to think of the all ones matrix as the identity, it is in fact not. And that's because matrix multiplication uses a different algorithm. You're not multiplying element by element, you're in fact adding a bunch of different elements together. So if I were to type mat rand two and multiply it by ones three, so what I'm doing again is multiplying a three by three matrix by the all ones matrix, also a three by three.
And when I press enter you see that I get a very different value. Those are some of the most useful types of special matrices that you can create in MATLAB.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions