Excel VBA Matrix Operations

K

Kirby Holte

How do I do matrix operations from within an Excel VBA macro?
Specifically matrix multiplication (MULT) and matrix inversion
(MINVERT).
 
D

Dana DeLouis

How do I do matrix operations from within an Excel VBA macro?

Hi. One way...

Sub Demo()
Dim a, m

a = [{3,4; 5,6}]

With WorksheetFunction
m = .MMult(a, a)
m = .MInverse(a)
End With
End Sub

= = = = = = =
HTH :>)
Dana DeLouis
 
K

Kirby

Thanks for the help.

Matrix Operations from within VBA

The following subroutine is called from another subroutine. Arrays p, vr,
vi, Qr, Qi and Pinv were dimensioned Dim p(), etc prior to the subroutines.
All arrays are Real. The matrix operation for Pinv() worked fine. The
operations for vr and vi arrays also worked fine. However, when calculations
got to Qr, I received an error message “Run Time Error ‘1001†unable to get
MMult property of the worksheetFunction.class. “nc†is 8 in the following.
Can anyone help?

Note, I wrote a very simple VBA routine to multiply a 3X3 times a 3X1 matrix
and called the resultant matrix “Câ€. C should be of dimensions 3X1. I
included a MsgBox(C(1)) and the program hung up. When I changed it to
MsgBox(C(1,1)), the program ran. Note, C was dimensioned (as a 1X3) … Dim
C(1 To 3) array.

Sub Qmatrix()
ReDim vr(1 To nc), vi(1 To nc), Qr(1 To nc), Qi(1 To nc), Pinv(1 To nc, 1 To
nc)
Pinv() = Application.WorksheetFunction.MInverse(p())

For j = 1 To nc
vr(j) = v(j) * Cos(va(j) * pi / 180)
vi(j) = v(j) * Sin(va(j) * pi / 180)
Next j

Qr = Application.WorksheetFunction.MMult(Pinv, vr)
Qi = Application.WorksheetFunction.MMult(Pinv, vi)

End Sub


Dana DeLouis said:
How do I do matrix operations from within an Excel VBA macro?

Hi. One way...

Sub Demo()
Dim a, m

a = [{3,4; 5,6}]

With WorksheetFunction
m = .MMult(a, a)
m = .MInverse(a)
End With
End Sub

= = = = = = =
HTH :>)
Dana DeLouis



Kirby said:
How do I do matrix operations from within an Excel VBA macro?
Specifically matrix multiplication (MULT) and matrix inversion
(MINVERT).
 
D

Dana DeLouis

ReDim .... Pinv(1 To nc, 1 To nc)
Pinv() = Application.WorksheetFunction.MInverse(p())

Hi. Not sure, but try using a variant as in the example below.
Good luck. Are you doing some variation of a Fourier analysis?

Sub Qmatrix()
Dim P As Variant
Dim Good As Variant
Dim Bad()
ReDim Bad(1 To 2, 1 To 2)

P = [{1,2; 3,4}]
Bad = WorksheetFunction.MInverse(P())

Good = WorksheetFunction.MInverse(P)
End Sub
Thanks for the help.

Matrix Operations from within VBA

The following subroutine is called from another subroutine. Arrays p, vr,
vi, Qr, Qi and Pinv were dimensioned Dim p(), etc prior to the subroutines.
All arrays are Real. The matrix operation for Pinv() worked fine. The
operations for vr and vi arrays also worked fine. However, when calculations
got to Qr, I received an error message “Run Time Error ‘1001†unable to get
MMult property of the worksheetFunction.class. “nc†is 8 in the following.
Can anyone help?

Note, I wrote a very simple VBA routine to multiply a 3X3 times a 3X1 matrix
and called the resultant matrix “Câ€. C should be of dimensions 3X1. I
included a MsgBox(C(1)) and the program hung up. When I changed it to
MsgBox(C(1,1)), the program ran. Note, C was dimensioned (as a 1X3) … Dim
C(1 To 3) array.

Sub Qmatrix()
ReDim vr(1 To nc), vi(1 To nc), Qr(1 To nc), Qi(1 To nc), Pinv(1 To nc, 1 To
nc)
Pinv() = Application.WorksheetFunction.MInverse(p())

For j = 1 To nc
vr(j) = v(j) * Cos(va(j) * pi / 180)
vi(j) = v(j) * Sin(va(j) * pi / 180)
Next j

Qr = Application.WorksheetFunction.MMult(Pinv, vr)
Qi = Application.WorksheetFunction.MMult(Pinv, vi)

End Sub


Dana DeLouis said:
How do I do matrix operations from within an Excel VBA macro?

Hi. One way...

Sub Demo()
Dim a, m

a = [{3,4; 5,6}]

With WorksheetFunction
m = .MMult(a, a)
m = .MInverse(a)
End With
End Sub

= = = = = = =
HTH :>)
Dana DeLouis



Kirby said:
How do I do matrix operations from within an Excel VBA macro?
Specifically matrix multiplication (MULT) and matrix inversion
(MINVERT).
 
K

Kirby

Thanks Dana,

I will give your suggestion a try on Thursday. Taking a day off tomorrow to
work on my boat.

The program is to compute various parameters for high voltage electric power
transmission lines ...

Kirby

Dana DeLouis said:
ReDim .... Pinv(1 To nc, 1 To nc)
Pinv() = Application.WorksheetFunction.MInverse(p())

Hi. Not sure, but try using a variant as in the example below.
Good luck. Are you doing some variation of a Fourier analysis?

Sub Qmatrix()
Dim P As Variant
Dim Good As Variant
Dim Bad()
ReDim Bad(1 To 2, 1 To 2)

P = [{1,2; 3,4}]
Bad = WorksheetFunction.MInverse(P())

Good = WorksheetFunction.MInverse(P)
End Sub
Thanks for the help.

Matrix Operations from within VBA

The following subroutine is called from another subroutine. Arrays p, vr,
vi, Qr, Qi and Pinv were dimensioned Dim p(), etc prior to the subroutines.
All arrays are Real. The matrix operation for Pinv() worked fine. The
operations for vr and vi arrays also worked fine. However, when calculations
got to Qr, I received an error message “Run Time Error ‘1001†unable to get
MMult property of the worksheetFunction.class. “nc†is 8 in the following.
Can anyone help?

Note, I wrote a very simple VBA routine to multiply a 3X3 times a 3X1 matrix
and called the resultant matrix “Câ€. C should be of dimensions 3X1. I
included a MsgBox(C(1)) and the program hung up. When I changed it to
MsgBox(C(1,1)), the program ran. Note, C was dimensioned (as a 1X3) … Dim
C(1 To 3) array.

Sub Qmatrix()
ReDim vr(1 To nc), vi(1 To nc), Qr(1 To nc), Qi(1 To nc), Pinv(1 To nc, 1 To
nc)
Pinv() = Application.WorksheetFunction.MInverse(p())

For j = 1 To nc
vr(j) = v(j) * Cos(va(j) * pi / 180)
vi(j) = v(j) * Sin(va(j) * pi / 180)
Next j

Qr = Application.WorksheetFunction.MMult(Pinv, vr)
Qi = Application.WorksheetFunction.MMult(Pinv, vi)

End Sub


Dana DeLouis said:
How do I do matrix operations from within an Excel VBA macro?

Hi. One way...

Sub Demo()
Dim a, m

a = [{3,4; 5,6}]

With WorksheetFunction
m = .MMult(a, a)
m = .MInverse(a)
End With
End Sub

= = = = = = =
HTH :>)
Dana DeLouis



Kirby Holte wrote:
How do I do matrix operations from within an Excel VBA macro?
Specifically matrix multiplication (MULT) and matrix inversion
(MINVERT).
 

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