Section 3.17. Comparing Two Matrices for Size

   

3.17 Comparing Two Matrices for Size

3.17.1 Problem

You want to compare two matrices to see whether they are equal in size. By equal in size, we mean that their highest X and Y dimensions are the same.

3.17.2 Solution

To compare matrices A and B for size, use the following query:

 SELECT m1.Matrix, 'is of equal size as', m2.Matrix FROM Matrices m1, Matrices m2 WHERE m1.X=m2.X AND m1.Y=m2.Y AND m1.Matrix='A' AND m2.Matrix='B' GROUP BY m1.Matrix, m2.Matrix  HAVING     COUNT(*)=(SELECT COUNT(*) FROM Matrices WHERE Matrix='A')     AND COUNT(*)=(SELECT COUNT(*) FROM Matrices WHERE Matrix='B') Matrix                                   Matrix                -------------------- ------------------- --------------------  A                    is of equal size as B 

3.17.3 Discussion

Some matrix operations require that the matrices involved are the same size. Use the query in this recipe to verify that such is the case.

First, we create two instances of the Matrices table (m1 and m2) and restrict each to one of the matrices that we are interested in. In our case, m1 represents matrix A, while m2 represents matrix B. If the matrices are equal, this will give us two rows for each combination of X and Y index values.

Next, in the WHERE clause, we match the coordinates of the two matrices. The GROUP BY clause is used so that query reports only one row of output. The results are grouped by the two matrix names . The HAVING clause then tests to ensure that the total number of rows summarized matches the total number of elements in A and B. If the totals all match, the two matrices are the same size.

   


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