Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2006, 06:31 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Matrix multiplication in VBA using arrays

Using VBA, I multiplied 2 arrays as below:

Dim X(1 To 3, 1 To 8) As Single
Dim Y(1 To 3, 1 To 8) As Single
Dim Z() As Single

X=.....
Y=.....

Z = Application.WorksheetFunction.MMult(X, Y)

The matrix multiplication did not work with Z As Single or Double only when it was Variant. I got a Type mismatch error, a similar thing happened if I defined the dimensions of Z (correctly). Why is this?

Is there a way round this if I want to multiply 2-D arrays using Mmult or some other method?

Variant arrays take up too much memory.

Many Thanks
 
Old June 26th, 2006, 03:15 PM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had a bit of a play around and came out with the following... I know this may not be an exact solution for you, but I hope you can addapt the code to help

Code:
    Dim X(1 To 8, 1 To 8) As Single
    Dim Y(1 To 8, 1 To 8) As Single
    Dim z(1 To 8, 1 To 8) As Single

' generating values for x and y
' enter your own values
    For a = 1 To 8
        For b = 1 To 8
            X(a, b) = Int(Rnd() * 10)
        Next
    Next

    For b = 1 To 8
        For a = 1 To 8
            Y(a, b) = Int(Rnd() * 10)
        Next
    Next


' populating z
    For a = 1 To 8
        For b = 1 To 8
            z(a, b) = Application.WorksheetFunction.MMult(X, Y)(a, b)
        Next
    Next





Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays for Controls in VBA Excel rduncan1 Excel VBA 3 October 3rd, 2010 11:10 PM
Control Arrays in VBA? Sanchin Excel VBA 3 March 24th, 2008 04:20 PM
matrix multiplication using wildcard in C/C++ rubnrj C++ Programming 0 March 5th, 2008 03:10 AM
Sum of sub-matrix of a matrix Gromlok Java Basics 0 March 4th, 2007 03:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.