# Transpose

 < Day Day Up >

### Transpose

The last matrix operation you need for future chapters is the transpose . This operation is used in Chapter 6 when we discuss the differences between OpenGL and DirectX. The transpose is a simple yet powerful operation. So far, we've used the row and column location to designate a particular entry. The transpose operation simply swaps each entry's row and column. The transpose can be applied to any size matrix, but we'll look at a 3x3 first so that you can quickly spot the pattern.

## Transpose for a 3 x 3 Matrix

NOTE

The T superscript is the symbol for transpose, so the transpose of matrix A is written as A T .

#### Example 5.10: Transpose of a 3 x 3 Matrix

##### Solution
1. The transpose operation flips each entry's row and column, so let's go through them one at a time. The 1 is in row 0, column 0, so when you flip the row and column, it stays the same. The 2, however, is in row 1, column 0, so the transpose sends it to row 0, column 1. The 3 goes from row 2, column 0 to row 0, column 2.

2. If you go through each entry and swap (transpose) the row and column, you'll find that everything flips about the diagonal, where the row is equal to the column. You should end up with a matrix that looks like this:

Notice that the transpose of a 3x3 matrix is another 3x3 matrix. If the original matrix is not square, the transpose changes the size, because the rows and columns are swapped. That means that a 3x1 matrix becomes a 1x3. This happens anytime you take the transpose of a vector.

#### Example 5.11: Transpose of a Vector

##### Solution
1. The transpose operation flips each entry's row and column, so let's go through them one at a time. The 4 is in row 0, column 0, so when you flip the row and column, it stays the same. The 5, however, is in row 1, column 0, so the transpose sends it to row 0, column 1. The 6 goes from row 2, column 0 to row 0, column 2.

2. This process leaves you with a single-row vector:

Again, the transpose can be applied to any size matrix. Just remember that the dimensions swap as well as each entry's row and column location.

## Transpose

For any size matrix A, each entry a mn moves to a nm in A T .

#### Example 5.12: Transpose of a Matrix

If , find D T .

##### Solution
1. The transpose operation flips each entry's row and column, so let's go through them one at a time. The 2 is in row 0, column 0, so when you flip the row and column, it stays the same. The 0, however, is in row 1, column 0, so the transpose sends it to row 0, column 1. Notice that the 0 and the 1 end up swapping positions .

2. You could continue to flip each entry one at a time. However, you might have noticed that the first row becomes the first column and the second row becomes the second column.

3. This process results in the following matrix:

This final operation is deceptively simple. Let's see how we can accomplish this in code to transpose a 4x4 matrix:

```Matrix4X4 transpose4X4Matrix(Matrix4X4 a)

{

Matrix4X4 temp;

for(int i = 0;i<4;i++)

{

for(int j=0;j<4;j++)

{

temp.index[i][j] = a.index[j][i];

}

}

return temp;

}
```

As soon as you have discovered an organized way to swap rows and columns, it can be calculated quickly. You might want to experiment with writing a function that calculates the transpose; all you need is a systematic method of swapping each entry's row and column. Again, we will revisit this operation in the next chapter, and you'll see just how useful it can be in programming.

#### Self-Assessment

For each of the following pairs of matrices, check to see if the product is defined. If it is, calculate the product.

 1 2 3 4 5 6

 < Day Day Up >