matrix multiplication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vbwire
    New Member
    • Jan 2008
    • 19

    #1

    matrix multiplication

    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?

    [CODE=vb]
    Private Sub Command1_Click( )

    Dim X(1 To 3, 1 To 3) As Integer
    Dim Y(1 To 3, 1 To 3) As Integer
    Dim Z(1 To 3, 1 To 3) As Integer
    Dim row As Integer
    Dim col As Integer


    For row = 1 To 3
    For col = 1 To 3
    Z(row, col) = (X(row, 1) * Y(1, col)) + (X(row, 2) * Y(2, col)) + (X(row, 3) * Y(3, col))

    Next
    Next

    txtZ(0 to 8)=Z(row, col)

    End Sub [/CODE]
    Last edited by debasisdas; Jan 23 '08, 06:33 AM. Reason: added code=vb tags
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

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

    Check This :

    [CODE=vb]
    Private Sub Command1_Click( )

    Dim X(1 To 3, 1 To 3) As Integer
    Dim Y(1 To 3, 1 To 3) As Integer
    Dim Z(1 To 3, 1 To 3) As Integer
    Dim row As Integer
    Dim col As Integer
    dim k As Integer
    k=0
    For row = 1 To 3
    For col = 1 To 3
    Z(row, col) = (X(row, 1) * Y(1, col)) + (X(row, 2) * Y(2, col)) + (X(row, 3) * Y(3, col))
    txtZ(k)=Z(row, col)
    k = k + 1
    Next
    Next

    End Sub [/CODE]

    Regards
    Veena

    Comment

    • vbwire
      New Member
      • Jan 2008
      • 19

      #3
      Code:
      A(1, 1) = txtA(0).Text
      A(1, 2) = txtA(1).Text
      A(1, 3) = txtA(2).Text
      A(2, 1) = txtA(3).Text
      A(2, 2) = txtA(4).Text
      A(2, 3) = txtA(5).Text
      A(3, 1) = txtA(6).Text
      A(3, 2) = txtA(7).Text
      A(3, 3) = txtA(8).Text
      
      B(1, 1) = txtB(0).Text
      B(1, 2) = txtB(1).Text
      B(1, 3) = txtB(2).Text
      B(2, 1) = txtB(3).Text
      B(2, 2) = txtB(4).Text
      B(2, 3) = txtB(5).Text
      B(3, 1) = txtB(6).Text
      B(3, 2) = txtB(7).Text
      B(3, 3) = txtB(8).Text
      is there any other way to load the value from the matrix? i use visual basic 6.0

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        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.

        Comment

        • QVeen72
          Recognized Expert Top Contributor
          • Oct 2006
          • 1445

          #5
          Hi,

          Try This :


          [CODE=vb]
          Dim i As Integer
          Dim j As Integer
          Dim k As Integer
          k= -1
          For i= 1 To 3
          For j = 1 To 3
          k = k + 1
          A(i , j) = txtA(k).Text
          B(i , j) = txtB(k).Text
          Next j
          Next i
          [/CODE]

          Regards
          Veena

          Comment

          • vbwire
            New Member
            • Jan 2008
            • 19

            #6
            this is what i programmed..

            Code:
            Private Sub cmdExit_Click()
            Unload Me
            End Sub
            
            Private Sub cmdXy_Click()
            
            Dim A(1 To 6, 1 To 6) As Integer
            Dim x(1 To 6, 1 To 4) As Integer
            Dim y(1 To 4, 1 To 6) As Integer
            Dim z(1 To 4, 1 To 6) As Integer
            Dim row As Integer
            Dim col As Integer
            
            
            x(1, 1) = txtX(0).Text
            x(1, 2) = txtX(1).Text
            x(1, 3) = txtX(2).Text
            x(1, 4) = txtX(3).Text
            x(2, 1) = txtX(4).Text
            x(2, 2) = txtX(5).Text
            x(2, 3) = txtX(6).Text
            x(2, 4) = txtX(7).Text
            x(3, 1) = txtX(8).Text
            x(3, 2) = txtX(9).Text
            x(3, 3) = txtX(10).Text
            x(3, 4) = txtX(11).Text
            x(4, 1) = txtX(12).Text
            x(4, 2) = txtX(13).Text
            x(4, 3) = txtX(14).Text
            x(4, 4) = txtX(15).Text
            x(5, 1) = txtX(16).Text
            x(5, 2) = txtX(17).Text
            x(5, 3) = txtX(18).Text
            x(5, 4) = txtX(19).Text
            x(6, 1) = txtX(20).Text
            x(6, 2) = txtX(21).Text
            x(6, 3) = txtX(22).Text
            x(6, 4) = txtX(23).Text
            
            y(1, 1) = txtY(0).Text
            y(1, 2) = txtY(1).Text
            y(1, 3) = txtY(2).Text
            y(1, 4) = txtY(3).Text
            y(1, 5) = txtY(4).Text
            y(1, 6) = txtY(5).Text
            y(2, 1) = txtY(6).Text
            y(2, 2) = txtY(7).Text
            y(2, 3) = txtY(8).Text
            y(2, 4) = txtY(9).Text
            y(2, 5) = txtY(10).Text
            y(2, 6) = txtY(11).Text
            y(3, 1) = txtY(12).Text
            y(3, 2) = txtY(13).Text
            y(3, 3) = txtY(14).Text
            y(3, 4) = txtY(15).Text
            y(3, 5) = txtY(16).Text
            y(3, 6) = txtY(17).Text
            y(4, 1) = txtY(18).Text
            y(4, 2) = txtY(19).Text
            y(4, 3) = txtY(20).Text
            y(4, 4) = txtY(21).Text
            y(4, 5) = txtY(22).Text
            y(4, 6) = txtY(23).Text
            
            z(1, 1) = txtZ(0).Text
            z(1, 2) = txtZ(1).Text
            z(1, 3) = txtZ(2).Text
            z(1, 4) = txtZ(3).Text
            z(1, 5) = txtZ(4).Text
            z(1, 6) = txtZ(5).Text
            z(2, 1) = txtZ(6).Text
            z(2, 2) = txtZ(7).Text
            z(2, 3) = txtZ(8).Text
            z(2, 4) = txtZ(9).Text
            z(2, 5) = txtZ(10).Text
            z(2, 6) = txtZ(11).Text
            z(3, 1) = txtZ(12).Text
            z(3, 2) = txtZ(13).Text
            z(3, 3) = txtZ(14).Text
            z(3, 4) = txtZ(15).Text
            z(3, 5) = txtZ(16).Text
            z(3, 6) = txtZ(17).Text
            z(4, 1) = txtZ(18).Text
            z(4, 2) = txtZ(19).Text
            z(4, 3) = txtZ(20).Text
            z(4, 4) = txtZ(21).Text
            z(4, 5) = txtZ(22).Text
            z(4, 6) = txtZ(23).Text
            
            
            
            If optXy.Value = True Then
                For row = 1 To 6
                    For col = 1 To 6
                        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))
                    Next
                Next
            Else
                For row = 1 To 6
                    For col = 1 To 6
                        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))
                    Next
                Next
            End If
            
            
            
            
            lbl11.Caption = A(1, 1)
            lbl12.Caption = A(1, 2)
            lbl13.Caption = A(1, 3)
            lbl14.Caption = A(1, 4)
            lbl15.Caption = A(1, 5)
            lbl16.Caption = A(1, 6)
            lbl21.Caption = A(2, 1)
            lbl22.Caption = A(2, 2)
            lbl23.Caption = A(2, 3)
            lbl24.Caption = A(2, 4)
            lbl25.Caption = A(2, 5)
            lbl26.Caption = A(2, 6)
            lbl31.Caption = A(3, 1)
            lbl32.Caption = A(3, 2)
            lbl33.Caption = A(3, 3)
            lbl34.Caption = A(3, 4)
            lbl35.Caption = A(3, 5)
            lbl36.Caption = A(3, 6)
            lbl41.Caption = A(4, 1)
            lbl42.Caption = A(4, 2)
            lbl43.Caption = A(4, 3)
            lbl44.Caption = A(4, 4)
            lbl45.Caption = A(4, 5)
            lbl46.Caption = A(4, 6)
            lbl51.Caption = A(5, 1)
            lbl52.Caption = A(5, 2)
            lbl53.Caption = A(5, 3)
            lbl54.Caption = A(5, 4)
            lbl55.Caption = A(5, 5)
            lbl56.Caption = A(5, 6)
            lbl61.Caption = A(6, 1)
            lbl62.Caption = A(6, 2)
            lbl63.Caption = A(6, 3)
            lbl64.Caption = A(6, 4)
            lbl65.Caption = A(6, 5)
            lbl66.Caption = A(6, 6)
            
            
            
            End Sub

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Populating the arrays can be coded in a much more compact way using loops. For instance...

              [CODE=vb]
              Private Sub cmdXy_Click()

              Dim A(1 To 6, 1 To 6) As Long
              Dim x(1 To 6, 1 To 4) As Long
              Dim y(1 To 4, 1 To 6) As Long
              Dim z(1 To 4, 1 To 6) As Long
              Dim row As Long
              Dim col As Long
              Dim Pointer As Long

              For row = 1 To 6
              For col = 1 To 4
              x(row, col) = txtX(Pointer).T ext
              Pointer = Pointer + 1
              Next
              Next

              Pointer = 0
              For row = 1 To 4
              For col = 1 To 6
              y(row, col) = txtY(Pointer).T ext
              z(row, col) = txtZ(Pointer).T ext
              Pointer = Pointer + 1
              Next
              Next[/CODE]
              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...
              [CODE=vb]
              For row = 1 To 6
              For col = 1 To 6
              Me.Control("lbl " & row & col).Caption = A(row, col)
              Next
              Next
              [/CODE]


              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.

              Comment

              • QVeen72
                Recognized Expert Top Contributor
                • Oct 2006
                • 1445

                #8
                Hi,

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

                [code=vb]
                Dim i As Integer
                Dim j As Integer
                Dim ctl As TextBox
                Dim TStr As String
                For i = 1 To 6
                For j = 1 To 6
                TStr = "lbl" & i & j
                Set ctl = Me.Controls(TSt r)
                ctl.Caption = A(i , j)
                Next j
                Next i
                [/code]

                Regards
                Veena

                Comment

                • vbwire
                  New Member
                  • Jan 2008
                  • 19

                  #9
                  thank you.. i got it.. terima kasih..

                  Comment

                  Working...