Join Curt Frye for an in-depth discussion in this video Transposing and inverting matrices, part of Up and Running with Octave.
If you do any machine learning applications using Octave, then you will definitely have to transpose and invert matrices. In this movie, I will show you the commands to do that, and go into a little bit of depth about what the results look like. That way you'll know what to expect when you use these operations later. Transposing matrices exchanges the columns and rows according to a specific procedure. In this case you see that I have an original matrix A, with the values 1 2 3 4 5 6, and a 2 by 3 matrix.
If you transpose the matrix, it become a 3 by 2, that meas the number of columns that you had before, becomes the number of rows. And the number of rows becomes the columns. So now I have, instead of 1, 2, 3, being in the top row, I have 1, 2, 3, in column number 1 on the left. And by the same token, I have 4, 5, 6, in the 2nd row in the original matrix, and 4, 5, 6, in the second column, in the transposed matrix. In octave you note the transposition operation, with a single quote, and you can see that to the right of transposed matrix A prime.
Inverting a new matrix is a little bit different. The idea behind inverting a matrix is that, if you were to multiply a matrix by its inverse, it would result in the identity matrix. What that means by implication, is that the matrix that you're inverting, must be a square matrix. Otherwise, you wouldn't be able to generate the identity matrix with the same number of rows and columns. So here, in this example, I have my original matrix, 1,2, 3, 4. And, after Octave calculates its inverse, minus 2, 1, 1.5, and minus 0.5.
If we multiply A, by the inverse of A, we get the 2 by 2 identity matrix, 1 0, 0 1. And again, the identity matrix is defined by having 1s on the top left to bottom right diagonal. And every other value being 0. With that overview in place, let's switch over to Octave and implement the commands there. Now that we're in Octave, I'll show you what the identity matrix generation command looks like. If I were to type in E Y E, which is the English word eye, and then 2, I would get a 2 by 2 diagonal, or identity matrix.
And you can see that the values of 1 occur on the top left to bottom right diagonal. And 0 occurs everywhere else. If it were 3 by 3, you would see the same pattern. Now let me quickly define a 2 by 2 matrix. I'll call it capital B, equals 1 2: 3 4. Right square bracket and Enter. And I see that I created it properly. To find the inverse of a matrix, you use the INV function.
So just type INV, a left parenthesis and then the name of the variable with the matrix you want to get the inverse for. It's capital B. Right parenthesis to close it, and Enter, and you see that I get the matrix minus 2, 1, 1.5, and minus 0.5. And if I were to multiply B by the inverse of B and press Enter, I get the identity matrix. Again, 1 on the top left to bottom right diagonal, and 0 everywhere else.
I want to point out, that there are some rare matrices that don't have an inverse. You probably won't run into them. But if you find the inverse function generates an error, or a message indicating the matrix doesn't have an inverse, you can use the PINV function, to calculate it's pseudo-inverse. What that looks like is, PINV of B, and again, B is in parentheses, and press Enter. And in this case, you get exactly the same value. In other cases, it will be different. So, for example, if I had matrix A equal to 1, 2, 3, 4, 5, 6, 7, 8, 9.
Again, it's very unlikely that you will run into a matrix that doesn't have an inverse, but if you do, just use PINV, and you can continue on.
- Downloading and installing Octave
- Using built-in commands
- Manipulating strings
- Defining vectors and matrices
- Defining functions
- Creating executable scripts
- Debugging your Octave code