Ba si si c Ma tr tr ix ix Op er era ti ti on ons wi wi th th VB .N .NET Pa Pa rt rt 3: 3: Tr Tr an ans po pos in ing Ma tr tr ic ic es es an and Ma Ma tr tri x Mul titi pl pl ic ic at ati on on - pa pa ul ul b. b. ....
http: // // pa paul bl bl as as er er. co co m/ m/ ba bas ic ic- ma ma tr tr ix ix- op op er er at ati on ons- wi wi th th- vb vb ne ne tt- pa pa rt rt- 33- tr tra
Join Follow Menu
paul blaser Analytics, bioinformatics, data mining, mining, GIS, health data, marketing technology technology and SQL Server
« Back to posts
November 23, 2011
Basic Matrix Operations with VB.NET Part 3: Transposing Matrices and Matrix Multiplication Transposing Transpo sing a m atrix is the process of transforming it so that its columns become rows and T
its rows become columns. A transposed matrix matrix is usually notated with ‘ or . So B transposed T
T
would be B’ or B . Here is our example matrix B transposed to B . Note that B has four T
columns and three rows, while B has three columns and four rows.
T
You can think of B 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 c an 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
Viewed 727 times
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
cannot multiply these two matrices.
T T However, our transposed matrix from above, B , has four rows. So we can multiply A and B T
since A has four columns and B has four rows.
The product of two matrices will have the number of rows in the first matrix and the number of T
columns in the second matrix. So C, the matrix that results from A x B , will have the number T
of rows in A and the number of columns in B . 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 T
row/column combination, add the products together. For example, multiplying A x B from above looks like this. T
Take the first row in A, 1 2 3 5, and the first column in B , 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 T
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
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
T
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 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 T
B , 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 T
Stick with the second row in A, 8 13 21 34, and now take the second column in B , 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 T
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 we’re finished with the second row in A, and we have the second row in our product matrix, C.
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
To calculate the third row in C, take the third row in A, 55 89 144 233, and the first c olumn in T
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 T Stick with the third row in A, 8 13 21 34, and now take the second column in B , 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 T
Again, stick with the third 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. (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 T to the example matrices A, B, B , and C.
Let’s begin by building A and B and populating the values. Sub Main()
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
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 T
B has four columns and three rows. Since B is the transpose of B, it has three columns and four rows.
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
Di m BT(3, 2) As I n t e ger T
We populate the values for B 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 T C is the product of A and B . It has three columns and three rows.
Di m C( 2, 2) As Integer We populate the values for C with nested FOR NEXT loops. Loops x and y correspond to the T
rows in A and the columns in B , respectively, and loop z corresponds to the columns in A and T
the rows in B . 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 T
Now write the matrices A, B, B and C to the screen.
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
For x = 0 To 2 For y = 0 To 3 Consol e. Wr i t e( A( x , y ) ) Consol e. Wr i t e(Space( 1)) Next Consol e. Wr i t eL i n e( ) Next Consol e. Wr i t eL i n e ( )
For x = 0 To 2 For y = 0 To 3 Consol e. Wr i t e( B( x , y ) ) Consol e. Wr i t e(Space( 1)) Next Consol e. Wr i t eL i n e( ) Next Consol e. Wr i t eL i n e ( )
For x = 0 To 3 For y = 0 To 2 Console. Wr i t e( BT( x , y ) ) Console. Wr i t e(Space( 1)) Next Console. Wr i t e Li n e( ) Next Console. Wr i t eL i n e ( )
For x = 0 To 2 For y = 0 To 2 Console. Wr i t e( C( x , y ) ) Console. Wr i t e(Space( 1)) Next
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra
Console. Wr i t e Li n e( ) Next
Consol e. Wr i t eL i n e ( )
End Sub
End Modul e And here is our output.
Download full size (22 KB)
0 responses Like Comment
Like
0
Tweet
Ba si c Ma tr ix Op era ti ons wi th VB .NET Pa rt 3: Tr ans pos ing Ma tr ic es and Ma tri x Mul ti pl ic ati on - pa ul b. ..
Leave a Comment Hello there. Don't believe we've met before! Log in with any of the following to comment:
Register or login to Posterous
http: // paul bl as er. co m/ bas ic- ma tr ix- op er ati ons- wi th- vb ne t- pa rt- 3- tra