ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using matrices is vba code (https://www.excelbanter.com/excel-programming/422378-using-matrices-vba-code.html)

Andrew[_56_]

Using matrices is vba code
 
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

Dave Peterson

Using matrices is vba code
 
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



Andrew wrote:

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


--

Dave Peterson


All times are GMT +1. The time now is 07:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com