473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

matrix multiplication

19
this program have 3 arrays.. x,y and z.. its multiply array x and array y and will display the answer in array z.. my problem is, how to make the answer displays at z?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  
  3. Dim X(1 To 3, 1 To 3) As Integer
  4. Dim Y(1 To 3, 1 To 3) As Integer
  5. Dim Z(1 To 3, 1 To 3) As Integer
  6. Dim row As Integer
  7. Dim col As Integer
  8.  
  9.  
  10. For row = 1 To 3
  11.     For col = 1 To 3
  12.         Z(row, col) = (X(row, 1) * Y(1, col)) + (X(row, 2) * Y(2, col)) + (X(row, 3) * Y(3, col))
  13.  
  14.     Next
  15. Next
  16.  
  17. txtZ(0 to 8)=Z(row, col)
  18.  
  19. End Sub 
Jan 23 '08 #1
8 6567
QVeen72
1,445 Expert 1GB
Hi,

To Display the Result also, you need to loop through:

Check This :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  
  3. Dim X(1 To 3, 1 To 3) As Integer
  4. Dim Y(1 To 3, 1 To 3) As Integer
  5. Dim Z(1 To 3, 1 To 3) As Integer
  6. Dim row As Integer
  7. Dim col As Integer
  8. dim k As Integer
  9. k=0
  10. For row = 1 To 3
  11.     For col = 1 To 3
  12.         Z(row, col) = (X(row, 1) * Y(1, col)) + (X(row, 2) * Y(2, col)) + (X(row, 3) * Y(3, col))
  13.       txtZ(k)=Z(row, col)
  14.       k = k + 1
  15.     Next
  16. Next
  17.  
  18. End Sub 
Regards
Veena
Jan 23 '08 #2
vbwire
19
Expand|Select|Wrap|Line Numbers
  1. A(1, 1) = txtA(0).Text
  2. A(1, 2) = txtA(1).Text
  3. A(1, 3) = txtA(2).Text
  4. A(2, 1) = txtA(3).Text
  5. A(2, 2) = txtA(4).Text
  6. A(2, 3) = txtA(5).Text
  7. A(3, 1) = txtA(6).Text
  8. A(3, 2) = txtA(7).Text
  9. A(3, 3) = txtA(8).Text
  10.  
  11. B(1, 1) = txtB(0).Text
  12. B(1, 2) = txtB(1).Text
  13. B(1, 3) = txtB(2).Text
  14. B(2, 1) = txtB(3).Text
  15. B(2, 2) = txtB(4).Text
  16. B(2, 3) = txtB(5).Text
  17. B(3, 1) = txtB(6).Text
  18. B(3, 2) = txtB(7).Text
  19. B(3, 3) = txtB(8).Text
  20.  
  21.  
  22.  
is there any other way to load the value from the matrix? i use visual basic 6.0
Jan 23 '08 #3
Killer42
8,435 Expert 8TB
I'm sure we can come up with much more compact (and possibly efficient) ways of doing this, using loops. But I'm short on time right now so I'll just say this...

If you are trying to place the results into the textboxes, then the statements coded in post #3 are backwards.

Also, I think line 12 in the original code posted in message #1 is probably wrong, but we can discuss that.
Jan 24 '08 #4
QVeen72
1,445 Expert 1GB
Hi,

Try This :


Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim j As Integer
  3. Dim k As Integer
  4. k= -1
  5. For i= 1 To 3
  6.     For j = 1 To 3
  7.        k = k + 1
  8.        A(i , j) = txtA(k).Text
  9.        B(i , j) = txtB(k).Text 
  10.     Next j
  11. Next i
  12.  
Regards
Veena
Jan 24 '08 #5
vbwire
19
this is what i programmed..

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExit_Click()
  2. Unload Me
  3. End Sub
  4.  
  5. Private Sub cmdXy_Click()
  6.  
  7. Dim A(1 To 6, 1 To 6) As Integer
  8. Dim x(1 To 6, 1 To 4) As Integer
  9. Dim y(1 To 4, 1 To 6) As Integer
  10. Dim z(1 To 4, 1 To 6) As Integer
  11. Dim row As Integer
  12. Dim col As Integer
  13.  
  14.  
  15. x(1, 1) = txtX(0).Text
  16. x(1, 2) = txtX(1).Text
  17. x(1, 3) = txtX(2).Text
  18. x(1, 4) = txtX(3).Text
  19. x(2, 1) = txtX(4).Text
  20. x(2, 2) = txtX(5).Text
  21. x(2, 3) = txtX(6).Text
  22. x(2, 4) = txtX(7).Text
  23. x(3, 1) = txtX(8).Text
  24. x(3, 2) = txtX(9).Text
  25. x(3, 3) = txtX(10).Text
  26. x(3, 4) = txtX(11).Text
  27. x(4, 1) = txtX(12).Text
  28. x(4, 2) = txtX(13).Text
  29. x(4, 3) = txtX(14).Text
  30. x(4, 4) = txtX(15).Text
  31. x(5, 1) = txtX(16).Text
  32. x(5, 2) = txtX(17).Text
  33. x(5, 3) = txtX(18).Text
  34. x(5, 4) = txtX(19).Text
  35. x(6, 1) = txtX(20).Text
  36. x(6, 2) = txtX(21).Text
  37. x(6, 3) = txtX(22).Text
  38. x(6, 4) = txtX(23).Text
  39.  
  40. y(1, 1) = txtY(0).Text
  41. y(1, 2) = txtY(1).Text
  42. y(1, 3) = txtY(2).Text
  43. y(1, 4) = txtY(3).Text
  44. y(1, 5) = txtY(4).Text
  45. y(1, 6) = txtY(5).Text
  46. y(2, 1) = txtY(6).Text
  47. y(2, 2) = txtY(7).Text
  48. y(2, 3) = txtY(8).Text
  49. y(2, 4) = txtY(9).Text
  50. y(2, 5) = txtY(10).Text
  51. y(2, 6) = txtY(11).Text
  52. y(3, 1) = txtY(12).Text
  53. y(3, 2) = txtY(13).Text
  54. y(3, 3) = txtY(14).Text
  55. y(3, 4) = txtY(15).Text
  56. y(3, 5) = txtY(16).Text
  57. y(3, 6) = txtY(17).Text
  58. y(4, 1) = txtY(18).Text
  59. y(4, 2) = txtY(19).Text
  60. y(4, 3) = txtY(20).Text
  61. y(4, 4) = txtY(21).Text
  62. y(4, 5) = txtY(22).Text
  63. y(4, 6) = txtY(23).Text
  64.  
  65. z(1, 1) = txtZ(0).Text
  66. z(1, 2) = txtZ(1).Text
  67. z(1, 3) = txtZ(2).Text
  68. z(1, 4) = txtZ(3).Text
  69. z(1, 5) = txtZ(4).Text
  70. z(1, 6) = txtZ(5).Text
  71. z(2, 1) = txtZ(6).Text
  72. z(2, 2) = txtZ(7).Text
  73. z(2, 3) = txtZ(8).Text
  74. z(2, 4) = txtZ(9).Text
  75. z(2, 5) = txtZ(10).Text
  76. z(2, 6) = txtZ(11).Text
  77. z(3, 1) = txtZ(12).Text
  78. z(3, 2) = txtZ(13).Text
  79. z(3, 3) = txtZ(14).Text
  80. z(3, 4) = txtZ(15).Text
  81. z(3, 5) = txtZ(16).Text
  82. z(3, 6) = txtZ(17).Text
  83. z(4, 1) = txtZ(18).Text
  84. z(4, 2) = txtZ(19).Text
  85. z(4, 3) = txtZ(20).Text
  86. z(4, 4) = txtZ(21).Text
  87. z(4, 5) = txtZ(22).Text
  88. z(4, 6) = txtZ(23).Text
  89.  
  90.  
  91.  
  92. If optXy.Value = True Then
  93.     For row = 1 To 6
  94.         For col = 1 To 6
  95.             A(row, col) = (x(row, 1) * y(1, col)) + (x(row, 2) * y(2, col)) + (x(row, 3) * y(3, col)) + (x(row, 4) * y(4, col))
  96.         Next
  97.     Next
  98. Else
  99.     For row = 1 To 6
  100.         For col = 1 To 6
  101.             A(row, col) = (x(row, 1) * z(1, col)) + (x(row, 2) * z(2, col)) + (x(row, 3) * z(3, col)) + (x(row, 4) * z(4, col))
  102.         Next
  103.     Next
  104. End If
  105.  
  106.  
  107.  
  108.  
  109. lbl11.Caption = A(1, 1)
  110. lbl12.Caption = A(1, 2)
  111. lbl13.Caption = A(1, 3)
  112. lbl14.Caption = A(1, 4)
  113. lbl15.Caption = A(1, 5)
  114. lbl16.Caption = A(1, 6)
  115. lbl21.Caption = A(2, 1)
  116. lbl22.Caption = A(2, 2)
  117. lbl23.Caption = A(2, 3)
  118. lbl24.Caption = A(2, 4)
  119. lbl25.Caption = A(2, 5)
  120. lbl26.Caption = A(2, 6)
  121. lbl31.Caption = A(3, 1)
  122. lbl32.Caption = A(3, 2)
  123. lbl33.Caption = A(3, 3)
  124. lbl34.Caption = A(3, 4)
  125. lbl35.Caption = A(3, 5)
  126. lbl36.Caption = A(3, 6)
  127. lbl41.Caption = A(4, 1)
  128. lbl42.Caption = A(4, 2)
  129. lbl43.Caption = A(4, 3)
  130. lbl44.Caption = A(4, 4)
  131. lbl45.Caption = A(4, 5)
  132. lbl46.Caption = A(4, 6)
  133. lbl51.Caption = A(5, 1)
  134. lbl52.Caption = A(5, 2)
  135. lbl53.Caption = A(5, 3)
  136. lbl54.Caption = A(5, 4)
  137. lbl55.Caption = A(5, 5)
  138. lbl56.Caption = A(5, 6)
  139. lbl61.Caption = A(6, 1)
  140. lbl62.Caption = A(6, 2)
  141. lbl63.Caption = A(6, 3)
  142. lbl64.Caption = A(6, 4)
  143. lbl65.Caption = A(6, 5)
  144. lbl66.Caption = A(6, 6)
  145.  
  146.  
  147.  
  148. End Sub
  149.  
  150.  
Jan 24 '08 #6
Killer42
8,435 Expert 8TB
Populating the arrays can be coded in a much more compact way using loops. For instance...

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdXy_Click()
  2.  
  3. Dim A(1 To 6, 1 To 6) As Long
  4. Dim x(1 To 6, 1 To 4) As Long
  5. Dim y(1 To 4, 1 To 6) As Long
  6. Dim z(1 To 4, 1 To 6) As Long
  7. Dim row As Long
  8. Dim col As Long
  9. Dim Pointer As Long
  10.  
  11. For row = 1 To 6
  12.   For col = 1 To 4
  13.     x(row, col) = txtX(Pointer).Text
  14.     Pointer = Pointer + 1
  15.   Next
  16. Next
  17.  
  18. Pointer = 0
  19. For row = 1 To 4
  20.   For col = 1 To 6
  21.     y(row, col) = txtY(Pointer).Text
  22.     z(row, col) = txtZ(Pointer).Text
  23.     Pointer = Pointer + 1
  24.   Next
  25. Next
I'm sure you could also combine these two sets of loops into a single one, but didn't have the time to play with it any more. In any case, one of the nice things about using loops like this is that if for example you decided to double the size of your array, the code hardly needs to be touched at all.

As for the actual calculation, I'm going to assume that your current code works. It could probably be rewritten to be a bit simpler by using another loop nested within row and col, which loops from 1 to 4. But it would run slower, and probably wouldn't be worth the effort.

Placing the results in the labels would also be much more compact if you make the labels into a control array. You can also "simulate" an array by building the control name dynamically. For example...
Expand|Select|Wrap|Line Numbers
  1. For row = 1 To 6
  2.   For col = 1 To 6
  3.     Me.Control("lbl" & row & col).Caption = A(row, col)
  4.   Next
  5. Next
  6.  

Of course, it's a matter of personal preference as to which way you want to code this stuff. You have to weigh up what's important to you. Various techniques will affect the length of the code, the complexity, performance, memory usage and so on.
Jan 25 '08 #7
QVeen72
1,445 Expert 1GB
Hi,

Use Killer's Code for Calculations and
Displaying of Results in Label can be done this way :

Expand|Select|Wrap|Line Numbers
  1. Dim i As Integer
  2. Dim j As Integer
  3. Dim ctl As TextBox
  4. Dim TStr As String
  5. For i = 1 To 6
  6.     For j = 1 To 6
  7.         TStr = "lbl" & i & j
  8.         Set ctl = Me.Controls(TStr)
  9.         ctl.Caption = A(i , j)
  10.     Next j
  11. Next i
  12.  
Regards
Veena
Jan 25 '08 #8
vbwire
19
thank you.. i got it.. terima kasih..
Jan 25 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
3
by: robix | last post by:
Hi again. I'm now asking your help because of a smal problem i'm getting with my multiplication matrix code. I'll try to give you as many details as possible. My matrix structure: typedef...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
6
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
3
by: crazygrey | last post by:
Hello, I'm a newbie to C++ so excuse me if my question was trivial but it is important to me. I'm implementing a simple code to find the forward kinematics of a robot: #include "stdafx.h"...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
8
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.