Sure it is. You're so very close!
Just remember what the transposition of a matrix actually involves - it involves reflecting the elements across the diagonal axis that runs from top-left to bottom-right.
That is to say:
[0 1] [0 2]
[2 3] [1 3]
[0 1 2] [0 3 6]
[3 4 5] [1 4 7]
[6 7 8] [2 5 8]
In each case, the row & column indicess of an element are
swapped unless the row index == column index.
So, you just need a single temporary variable of whatever type your matrix holds. Its just a series of (N^2 - N) swap operations, where N is the number of rows or columns - assuming a square matrix.
Put the contents of [row][column] into tmpVal
Set the contents of [row][column] to be [column][row]
Set the contents of [column][row] to be tmpVal
Naturally, this doesn't take into account the fact that we don't have to copy elements if column==row. Its actually probably quicker just to blast through, never comparing the column and the row. Theres relatively few element this will be true for - worst case is 50%, when you've got a 2x2 matirx - or for a 10x10 matrix, it will be 10 elements. This can be calculated (for a square matrix) by N/(N^2) So, as the matrix grows larger, the potential savings get ever smaller.
Yet, if you try to do this 'optimization' you'll find yourself comparing the row to the column for 100% of the elements.