Team LiB   Previous Section   Next Section

3.15 Transposing a Matrix

3.15.1 Problem

You want to transpose a matrix. To transpose a matrix, swap all X and Y values. For example, an element located at X=1, Y=2 will be swapped with the element located at X=2, Y=1.

3.15.2 Solution

Follow the pattern used for the following query, which transposes matrix D:

SELECT Y AS X, X AS Y, Value 
FROM Matrices
WHERE Matrix='D'

X           Y           Value       
----------- ----------- ----------- 
1           1           3
2           2           4
3           3           5
1           1           5
2           2           6
3           3           7
1           1           8
2           2           9
3           3           0

3.15.3 Discussion

Transposition is probably one of the easiest operations on matrices. The only thing you have to do is to report X as Y and Y as X, and that transposes the matrix. If you wish to store your transposition — this recipe only prints the transposed version — you can write an INSERT . . . SELECT . . . FROM statement:

INSERT INTO Matrices
SELECT 'Dt',Y, X, Value 
FROM Matrices
WHERE Matrix='D'

This statement transposes matrix D and stores the results in a new matrix named Dt.

    Team LiB   Previous Section   Next Section