By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

SQL Techniques for Performing Operations on Matrices : Page 3

SQL does not support direct operations on matrices, but it does allow easy manipulations with matrices. Learn a few SQL techniques for performing some basic operations on matrices.

4) Matrices Multiplication
Two matrices (A and B) can be multiplied if they are compatible, meaning that the first (left) matrix has as many columns as the second (right) matrix has rows. If matrices A and B meet this condition, you can multiply them. The result of such a multiplication will be matrix C, which has as many rows as matrix A and as many columns as matrix B:

A_{mxn} ∙B_{nxp} = C_{mxp}

The rules for matrices multiplication are not as trivial as for scalar matrix multiplication. To calculate the ij-element of the product matrix, you need to multiply the i^{th} row vector of the first matrix by the j^{th} column vector of the second matrix. The multiplication of a row vector by a column vector goes as follows:

1. Multiply the first elements of the row and column vectors to get product 1.
2. Multiply the second elements of the row and column vectors to get product 2.
. . . . . . . . . . . . .
N. Multiply the Ns elements of the row and column vectors to get product N.
Lastly, sum up all the products.

The result of such a calculation will be a scalar.

Figure 2 shows the algorithm in math notation for A_{mxn} ∙B_{nxp} = C_{mxp} (where i = 1, 2, . . m, and j =1, 2, . . p.).

Figure 2. Calculating ij-element of Product Matrix C: Here is the algorithm in math notation for A_{mxn} ∙B_{nxp} = C_{mxp}.

To find product C = A ∙ B, where:

You would use this solution:

Matrices multiplication is not commutative (i.e., A∙B ≠ B∙A). Indeed, in Example 4, the multiplication A_{3x2} ∙ B_{2x4} is allowed, because matrices A_{3x2} and B_{2x4} are compatible. However, the multiplication B_{2x4} ∙ A_{3x2} would be illegal, because the number of columns in the left matrix is not equal to the number of rows in the right matrix. But even if both A∙B and B∙A are compatible, that doesn’t guarantee the same result.

Consider two square matrices:

However, matrices multiplication is associative and distributive:

associative - A∙(B∙C) = (A∙B)∙C

distributive - A∙(B + C) = A∙B + A∙C

distributive - (A + B)C = A∙C + B∙C

5) Matrix Transpose
The transposing of a matrix simply means an exchange between the rows and the columns. Suppose you have an m x n matrix, A_{mxn}. If you transpose that matrix, you will get an n x m matrix, B_{nxm}, with the following correspondence between the elements of A and B:

a_{ixj} = b_{jxi}, for all i and j.

The most common notation for the transposing of matrix A is A^{T}.