## Saturday, February 16, 2013

### Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication

Transposing a matrix is the process of transforming it so that its columns become rows and its rows become columns.  A transposed matrix is usually notated with ‘ or  T. So B transposed would be B’ or BT. Here is our example matrix B transposed to BT. Note that B has four columns and three rows, while BT has three columns and four rows. You can think of BT visually as the result of rotating B 90 degrees counter clockwise.

Multiplying two matrices is less straightforward than adding or subtracting. As we saw in part 2, you can add two matrices if they each have the same number of rows and columns. However, you can multiply two matrices only if the number of columns in matrix A is equal to the number of rows in matrix B.

Using our example matrices A and B, A has 4 columns and B has three rows. Since 4≠3 we cannot multiply these two matrices. However, our transposed matrix from above, BT, has four rows. So we can multiply A and BT since A has four columns and BT has four rows. The product of two matrices will have the number of rows in the first matrix and the number of columns in the second matrix. So C, the matrix that results from A x BT, will have the number of rows in A and the number of columns in BT. C will be a 3 by 3 matrix. This is sometimes notated as C3×3 or C3,3

The trick to multiplying two matrices is to take each number in each row in the first matrix and multiply it by each corresponding number in each column in the second matrix.  For each row/column combination, add the products together. For example, multiplying A x BT from above looks like this.

Take the first row in A, 1 2 3 5, and the first column in BT, 2 3 5 7, multiply them together and add the products, like this.

(1 x 2) + (2 x 3) + (3 x 5) + (5 x 7) = 2 + 6 + 15 + 35 = 58

Stick with the first row in A, 1 2 3 5, and now take the second column in BT, 11 13 17 19, multiply them together and add the products, like this.

(1 x 11) + (2 x 13) + (3 x 17) + (5 x 19) = 11 + 26 + 51 + 95 = 183

Again, stick with the first row in A, 1 2 3 5, and now take the third column in BT, 23 29 31 37, multiply them together and add the products, like this.

(1 x 23) + (2 x 29) + (3 x 31) + (5 x 37) = 23 + 58 + 93 + 185 = 359

Good. Now we’re finished with the first row in A, and we have the first row in our product matrix, C. To calculate the second row in C, take the second row in A, 8 13 21 34, and the first column in BT, 2 3 5 7, multiply them together and add the products, like this.

(8 x 2) + (13 x 3) + (21 x 5) + (34 x 7) = 16 + 39 + 105 + 238 = 398

Stick with the second row in A, 8 13 21 34, and now take the second column in BT, 11 13 17 19, multiply them together and add the products, like this.

(8 x 11) + (13 x 13) + (21 x 17) + (34 x 19) = 88 + 169 + 357 + 646 = 1260

Again, stick with the second row in A, 8 13 21 34, and now take the third column in BT, 23 29 31 37, multiply them together and add the products, like this.

(8 x 23) + (13 x 29) + (21 x 31) + (34 x 37) = 184 + 377 + 651 + 1258 = 2470

Good again. Now we’re finished with the second row in A, and we have the second row in our product matrix, C. To calculate the third row in C, take the third row in A, 55 89 144 233, and the first column in BT, 2 3 5 7, multiply them together and add the products, like this.

(55 x 2) + (89 x 3) + (144 x 5) + (233 x 7) = 110 + 267 + 720 + 1631 = 2728

Stick with the third row in A, 8 13 21 34, and now take the second column in BT, 11 13 17 19, multiply them together and add the products, like this.

(55 x 11) + (89 x 13) + (144 x 17) + (233 x 19) = 605 + 1157 + 2448 + 4427 = 8637

Again, stick with the third row in A, 8 13 21 34, and now take the third column in BT, 23 29 31 37, multiply them together and add the products, like this.

(55 x 23) + (89 x 29) + (144 x 31) + (233 x 37) = 1265 + 2581 + 4464 + 8621 = 16931

Great. Now we’re finished with the third row in A, and we have the final row in our product matrix, C. In the code for the calculations above we will build four arrays, A, B, BT and C, corresponding to the example matrices A, B, BT, and C.

Let’s begin by building A and B and populating the values.

Sub Main()
Dim A(2, 3) As Integer
A(0, 0) = 1
A(0, 1) = 2
A(0, 2) = 3
A(0, 3) = 5
A(1, 0) = 8
A(1, 1) = 13
A(1, 2) = 21
A(1, 3) = 34
A(2, 0) = 55
A(2, 1) = 89
A(2, 2) = 144
A(2, 3) = 233

Dim B(2, 3) As Integer
B(0, 0) = 2
B(0, 1) = 3
B(0, 2) = 5
B(0, 3) = 7
B(1, 0) = 11
B(1, 1) = 13
B(1, 2) = 17
B(1, 3) = 19
B(2, 0) = 23
B(2, 1) = 29
B(2, 2) = 31
B(2, 3) = 37

B has four columns and three rows. Since BT is the transpose of B, it has three columns and four rows.

Dim BT(3, 2) As Integer

We populate the values for BT with nested FOR NEXT loops. The outer loop, x, corresponds to the rows in each array and the inner loop, y, corresponds to the columns in each array.  Populate the rows in BT with the columns from B and the columns in BT with the rows from B.

For x = 0 To 3
For y = 0 To 2
BT(x, y) = B(y, x)
Next
Next

C is the product of A and BT. It has three columns and three rows.

Dim C(2, 2) As Integer

We populate the values for C with nested FOR NEXT loops. Loops x and y correspond to the rows in A and the columns in BT, respectively, and loop z corresponds to the columns in A and the rows in BT.  As mentioned above, the trick to multiplying two matrices is to take each number in each row in the first matrix and multiply it by each corresponding number in each column in the second matrix. The loops look like this.

For x = 0 To 2
For y = 0 To 2
For z = 0 To 3
C(x, y) = C(x, y) + A(x, z) * BT(z, y)
Next
Next
Next

Now write the matrices A, B, BT and C to the screen.

For x = 0 To 2
For y = 0 To 3
Console.Write(A(x, y))
Console.Write(Space(1))
Next
Console.WriteLine()
Next
Console.WriteLine()

For x = 0 To 2
For y = 0 To 3
Console.Write(B(x, y))
Console.Write(Space(1))
Next
Console.WriteLine()
Next
Console.WriteLine()

For x = 0 To 3
For y = 0 To 2
Console.Write(BT(x, y))
Console.Write(Space(1))
Next
Console.WriteLine()
Next
Console.WriteLine()

For x = 0 To 2
For y = 0 To 2
Console.Write(C(x, y))
Console.Write(Space(1))
Next
Console.WriteLine()
Next
Console.WriteLine()
End Sub

End
Module

And here is our output.