Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Operations on a matrix 1

Status
Not open for further replies.

robert693

Programmer
Jun 20, 2001
40
US
I have a 20 x 30 matrix in a two dimensional array. I have to do mathmatical operations on it where each element of the matrix is multiplied by the corresponding element in the next row. For example if it were a 3 x 4 matrix
2 3 5 1
7 5 8 2
1 4 8 2
The 2 would be multiplied by the 7 then the 3 by the 5.... After the first row is complete, the next row is multiplied to the third, 7 x 1 then
5 x 4 and so on. I cannot figure out what combinations of for loops will accomplish this task. Any help would be greatly appreciated.
 
With the size opf your matrix, I'm sure you can have more issues than are addressed in the following, and sans understanding of the overall app, I am not going very far w/ checking / trapping the potential exceptions.

At least the loopy part can be seen to work, and should, w/o to much trounble, get a paeeing grade.



Code:
Public Function txtMatrixProd()


    Dim MyMatrix(2, 3) As Integer
    Dim MyRtn As Variant
    Dim Idx As Integer
    Dim Jdx As Integer

    MyMatrix(0, 0) = 2
    MyMatrix(0, 1) = 3
    MyMatrix(0, 2) = 5
    MyMatrix(0, 3) = 1

    MyMatrix(1, 0) = 7
    MyMatrix(1, 1) = 5
    MyMatrix(1, 2) = 8
    MyMatrix(1, 3) = 2

    MyMatrix(2, 0) = 1
    MyMatrix(2, 1) = 4
    MyMatrix(2, 2) = 8
    MyMatrix(2, 3) = 2

'    'Document what was Sent
'    While Idx <= UBound(MyMatrix, 1)
'        Jdx = 0                         'Initalize the Row Count
'        While Jdx <= UBound(MyMatrix, 2)
'
'            Debug.Print MyMatrix(Idx, Jdx); Space(5);
'
'            Jdx = Jdx + 1
'        Wend
'        Debug.Print
'        Idx = Idx + 1
'    Wend

    'Get it Back

    'Show the results
    MyRtn = basMatrixProd(MyMatrix)
    While Idx <= UBound(MyRtn)
        Debug.Print Idx, MyRtn(Idx)
        Idx = Idx + 1
    Wend

End Function


Code:
Public Function basMatrixProd(AryIn() As Integer) As Variant

    'Michael Red    5/1/04
    'Return an aray ofthe product of the columns of an array
    '2      3   5       1 _
     7      5   8       2 _
     1      4   8       2

    'Should Return _
     14    60   320     4

    '(or at least so I think)

    Dim Col As Integer
    Dim Row As Integer
    Dim MyProd() As Variant

    ReDim MyProd(UBound(AryIn, 2))      'Create an element for each Column
    On Error GoTo BadVal                'Exit on errors

    Row = 0                             'Initalilze the Rowumn Count
    While Col <= UBound(AryIn, 2)
        MyProd(Col) = 1                     'Initalize the Col value
        Row = 0                             'Initalize the Row Count
        While Row <= UBound(AryIn, 1)
            If (Not IsNull(AryIn(Row, Col))) Then               'Check for bad info (Null)
                MyProd(Col) = MyProd(Col) * AryIn(Row, Col)     'Extend the Product
            End If
            Row = Row + 1
        Wend
        Col = Col + 1
    Wend


NormExit:
    basMatrixProd = MyProd()
    Exit Function

BadVal:
    MyProd(0) = -1
    GoTo NormExit

End Function







MichaelRed
mlred@verizon.net

 
MichaelRed,

I gave you a star for your generosity and effort in answering the question.

There are many answers, including doing some vector maths, but your generosity has no dimensions!!!

Cheers.

&quot;Life is full of learning, and then there is wisdom&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top