Join Curt Frye for an in-depth discussion in this video Transposing and inverting matrices, part of Mathematica 10 Essential Training.

- When you use Mathematica for machine learning tasks, you will often need to transpose matrices. That is, change the position of values within a matrix according to a rule, and also to find the inverse of matrices. In this movie, we'll show you how to perform both of those tasks. I'll start by showing you visually what transposing a matrix looks like. In this case, on the left, I have a matrix that is a 2 X 3 matrix with the values 1, 2, 3 in row one, and 4, 5, 6, in row two.

Now let's see what the transposed matrix, which is called A prime, looks like. When you transpose a matrix, the number of rows in the first matrix becomes the number of columns in the second, and so on. You can also see that the first row has been moved to the left-most column, and the second row, moved to the right-most column. So that's what transposing a matrix looks like. And again, it is extremely useful in machine learning. When we talk about inverting matrices, we're doing something different. In this case, we have our Original matrix A, and if we want to find the inverted matrix for A, we would get this result.

- 2, 1, 1.5, and -0.5. So the question is, how are those two matrices related? And the answer, is that if you multiply a matrix by its inversion, then you get the identity matrix. And the identity matrix is a matrix that has the value 1 in all of the matrix spots along what's called the main diagonal. From position 1, 1 at the top left, to the final position at the end of the last row.

And the 1's continue along the diagonal the entire way. That means to find the inverse of a matrix, you need to be working with a square matrix. Otherwise, you can't get an identity matrix like you see at the bottom of the slide. With that introduction in mind, let's switch over to Mathematica, and transpose and invert matrices there. I'm working in a blank Mathematica notebook, so I need to enter in a matrix to work with. I will call the matrix mat2, then equals sign, and remember, a matrix is a series of nested lists, so I need to start the master list, by typing a left curly bracket and then the first row, by typing a second left curly bracket.

And I'll type the numbers 1, 2, 3, right curly bracket to end the first row, then a comma, left curly, 4, 5, 6, right curly, comma, then a space, left curly bracket, and now the final row, 7, 8, 9, now two right curly brackets, one to close the row, one to close the list, shift enter, and there I see my list. If I want to see what that list looks like in matrix form, in other words as a grid, then I can assign it to a new variable, I'll just call it mat2m, equals MatrixForm, left square bracket of the variable mat2, right square bracket, shift enter, and there I see it as a matrix.

And just as a reminder, I'm assigning the matrix form of this particular variable to a new variable, because sometimes you can run into issues with multiplication or addition when you have a matrix stored and matrix form, as opposed to a list. Now let's say that I want to transpose Matrix 2. To do that, I can use the transpose keyword. So I will type mat2 then t, and to me, this means Matrix 2 transposed, I'm not following any formal naming convention here, but it helps me keep in my mind what each variable contains, then I'll type equal, and then transpose, left square bracket, mat2, then right square bracket, and shift enter, and I see the transposition.

And if I want to see that in matrix form, I can do mat2tm, so transposed in matrix form, equal, then MatrixForm, left square bracket, mat2t, right square bracket, shift enter, and there I see the transposition. And indeed, I see that the first column of the transposed matrix equals the first row of the original matrix and so on. Now let's say that I want to find the inverse of a matrix.

To do that, I need to create a square matrix, so I'll type mat3, equals, and then I'll just create a 2 X 2 matrix, so I'll start with two left curly brackets to indicate the start of the master list, and also the first row, so I'll type 1, 4, then a right curly bracket, comma, left curly bracket to start a second row, 5, 10, right curly bracket and a second right curly bracket to end the second row, and also the general list, shift enter, and there it is, and now I can find the inverse of this matrix.

So, I will say mat3i, equals inverse, left square bracket, mat3, right square bracket and shift enter, and I see that I have the inversion of this particular matrix. I can display that in matrix form, by assigning two new variables, so let's say I have mat3im, that's the inverse in matrix form, equals MatrixForm, left square bracket, mat3i, right square bracket, shift enter, and there I see it.

I can display this matrix as numbers instead of fractions, by using the "N" keyword. So if I type N, left square bracket, mat3in, and a right square bracket, and shift enter, and I get the values displayed numerically, instead of as fractions. Now I'll show you what happens if I multiply Matrix 3 by the inverse of Matrix 3. So if I type mat3, then a period, which is used for matrix multiplication, by mat3i, and shift enter, I get a matrix with one 0 on the first level, and 0, 1 on the second.

And if I do the same thing, sending the result to matrix form, so MatrixForm of mat3, dot mat3i, right square bracket and shift enter, you see that I get a 2 X 2 identity matrix back. Finally, I would like to point out that it is rare, but there will be occasions where you will create a matrix that does not have an inverse. Like I said, it is rare, but it does happen. Those matrices are called singular.

The first matrix I created, mat2, is one such matrix. So if I were to take the inverse of that I could say mat2i equals inverse, left square bracket of mat2, then a right square bracket and shift enter, I get an error indication, telling me that that matrix is in fact singular and it does not have an inverse. So if you ever see this message, you have found a matrix that does not have an inverse.

###### Released

6/24/2015*Mathematica 10 Essential Training*teaches information workers how to analyze data using the Mathematica 10 environment and language. After completing the course, members will be able to set up Mathematica notebooks, import data, use operators to perform simple calculations, create and manipulate lists, perform matrix calculations, analyze data using descriptive statistics, and write and debug Mathematica scripts. Author Curt Frye also shows how to visualize data with charts, convert files into the Computable Document Format, and add animation to make your results more interactive.

- Managing notebooks
- Working with operators
- Assigning values to variables
- Importing and exporting data
- Creating advanced formulas
- Creating and manipulating lists
- Manipulating arrays
- Analyzing data with descriptive analytics
- Manipulating matrices
- Managing scripts
- Creating charts
- Formatting data

## Share this video

## Embed this video

Video: Transposing and inverting matrices