Consulting

Results 1 to 4 of 4

Thread: Urgent help re interpretation of code

  1. #1
    VBAX Newbie
    Joined
    Oct 2010
    Posts
    1
    Location

    Urgent help re interpretation of code

    Hello

    I am relatively new to using vba. I have some code which I need to interpret re matrices.
    1) explain the nature of the output matrix based on the procedure in the code
    2) verify that all matrix multiplications are valid in terms of matrix size compatibility

    Function Matrix_1(y As Variant, X As Variant) As Variant
    Dim Xtrans As Variant
    Dim temp As Variant
    Dim output() As Variant, r() As Double, t() As Double
    Xtrans = Application.Transpose(X)
    XtX = Application.MInverse(Application.MMult(Xtrans, X))
    temp = Application.Mult(XtX, Xtrans)
    temp_out = Application.MMult(btemp, y)
    Result = temp_out
    End Function

    Please help! thanks!

  2. #2
    Hello,
    Well, I may be a VBA newbie aswell, but here's the way I see what's happening in that code.
    [vba]Function Matrix_1(y As Variant, X As Variant) As Variant
    [/vba]
    Here you're just defining the variables to be passed to the function (y and X ), and what the return value will be (in this case a variant, which I gather is the object used to contain matrices in VBA ).

    [vba]Dim Xtrans As Variant
    Dim temp As Variant
    Dim output() As Variant, r() As Double, t() As Double
    [/vba]
    Defining all the variables to be stored in the RAM and used in the function. All of them will probably be matrices, except for r and t .
    Note : being a newbie, I'm unsure as to why these variables seem to be methods due to the "()" at the end.

    [vba]Xtrans = Application.Transpose(X)
    [/vba]
    Transposing the X matrix, and storing it in Xtrans.

    [vba]XtX = Application.MInverse(Application.MMult(Xtrans, X))
    [/vba]
    Here, you are taking the inverse of the product of XTrans ( the x matrix you previously transposed ) and X, the original matrix used for Xtrans.

    [vba] temp = Application.Mult(XtX, Xtrans)[/vba]
    Again, a multiplication. As it is not MMult, but in this case just Mult, I doubt very much that this is a matrix multiplication. Probably a scalar one ? Though don't see how that works with two matrices as arguments.
    Value is being stored in variable named temp.

    [vba]temp_out = Application.MMult(btemp, y)
    Result = temp_out
    [/vba]
    Doing a Matrix multiplication between btemp (surely this must mean the temp variable ? ) and y, one of the arguments you passed the function.
    I think it's not necessary to store it in temp_out then Result, but then again, I'm a VBA newbie, so there might be some silly reason why that has to be done.


    As far as I can see
    If the function works, then I am baffled, as I do not see much logic to it. If however, Result is the way that the result is returned from the function ( was pretty sure you would have had to store that in a variable named Matrix_1 ), and ASSUMING that btemp is suppose to be temp and that the code was meant to use MMult instead of Mult on the 7th line of your code, then this would be the matrix calculation being done :

    Matrix arguments x and y
    Result = ( ( ( x(T) * x )(^-1) ) * x(T) )* y

    Where
    x(T) means the transpose of the matrix named x
    (^-1) means the inverse of the matrix in the brackets beforehand.

    Hope this helps as a basis for understanding/discussion while you await an answer from someone far more versed in the mystery that is VBA

    P.S : my original post was going to contain links to pages for you to understand what the different matrix calculations do, but not allowed to post links until I have done at least 5 posts

    ---------------------------------------------
    Light travels faster than sound. That is why some people appear to be smart until you hear them speak

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    This looks like a homework project, and it is not appropriate for us to do your homework.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    The best way to get help with your project is to use Excel's Help. Insert a module in the VBE and paste the code. Press F1 with the cursor in each keyword. This is the best way to learn about pre-built functions while in the VBE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •