Section 3.15. Transposing a Matrix

   

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.

   


Transact-SQL Cookbook
Transact-SQL Cookbook
ISBN: 1565927567
EAN: 2147483647
Year: 2005
Pages: 152

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net