Using matrices is vba code

A

Andrew

Hello,
I am trying to do some simple matrix manipulation in vba. Here's what
I have:

A = 4x4 matrix, B = a 4x1 column, and C= a 4x1 column. declared as
Dim A(1 To 4, 1 To 4) As Double
Dim B(1 To 4) As Double
Dim C(1 To 4) As Double

What I want to do is invert A, then multiply it by B, and put the
results in C.

Functionally, my code is doing this... C = (A^1)*B
Of course vba doesn't understand what I want it to do. I have tried
to implement the code as

C=MInverse(A) - code won't get past here.
C=MMult(C,B)

Can someone send me an example of how to implement C = (A^1)*B

thanks,
Andy
 
D

Dave Peterson

One way:

Option Explicit
Sub testme()

Dim i As Long
Dim j As Long

Dim A(1 To 4, 1 To 4) As Double
Dim B(1 To 4) As Double
Dim C As Variant
Dim D As Variant

For i = LBound(A, 1) To UBound(A, 1)
For j = LBound(A, 2) To UBound(A, 2)
A(i, j) = Rnd
Next j
Next i

For i = LBound(B) To UBound(B)
B(i) = Rnd
Next i


With Application
C = .MInverse(A)
D = .MMult(C, .Transpose(B))

'or in one expression
D = .MMult(.MInverse(A), .Transpose(B))
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top