Basic Matrix Operations With VB - Net Part 3 - Transposing
Basic Matrix Operations With VB - Net Part 3 - Transposing
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
paul blaser
Analytics, bioinformatics, data mining, GIS, health data, marketing technology and SQL Server
Back to posts
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 B . Here is our example matrix B transposed to B . Note that B has four columns and three rows, while B has three columns and four rows.
T T T
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 43 we
1 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
However, our transposed matrix from above, BT , has four rows. So we can multiply A and BT since A has four columns and B has four rows.
T
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 B , will have the number of rows in A and the number of columns in B . C will be a 3 by 3 matrix. This is sometimes notated as C33 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 B 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 B , 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
T T T T
2 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
Again, stick with the first row in A, 1 2 3 5, and now take the third column in B , 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 were 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 B , 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 were finished with the second row in A, and we have the second row in our product matrix, C.
T
3 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
To calculate the third row in C, take the third row in A, 55 89 144 233, and the first column in B , 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 were finished with the third row in A, and we have the final row in our product matrix, C.
T
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. Lets begin by building A and B and populating the values.
Sub Main()
4 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
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
5 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
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.
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.
6 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
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
7 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
Like
Tweet
8 9
23/10/2012 12:57
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication - paul b...
http://paulblaser.com/basic-matrix-operations-with-vbnet-part-3-tra
Leave a Comment
Hello there. Don't believe we've met before!
Log in with any of the following to comment:
9 9
23/10/2012 12:57