# Matrix Transpose

All square matrices have a Main Diagonal. This is the row of numbers that goes diagonally along the matrix from the top left to the bottom right. For a 4x4 matrix, these are elements: (0,0), (1,1), (2,2), (3,3). Another way to define the main diagonal is elements which have the row and column index.

$$\begin{bmatrix} \textbf{A} & e & i & m\\ b & \textbf{F} & j & n\\ c & g & \textbf{K} & o\\ d & h & l & \textbf{P} \end{bmatrix}$$

To transpose a matrix, flip it along it's main diagonal. The image below shows the transpose operation.

You may have noticed that transposing a Column Major matrix gives it the same logical topology as if it where converted to a Row Major matrix. The transpose operation can be used to change the logical topology of a matrix. The code sample below shows a simple Transposed function.

mat4 Transposed(mat4 m) {
mat4 r;

r.v[0] = m.v[0];
r.v[1] = m.v[3];
r.v[2] = m.v[6];

r.v[3] = m.v[1];
r.v[4] = m.v[4];
r.v[5] = m.v[7];

r.v[6] = m.v[2];
r.v[7] = m.v[5];
r.v[8] = m.v[8];

return r;
}

// mat 2 and 3 transpose functions are similarly trivial