• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

MMULT, TRANSPOSE AND TOCOL

Yash0071

New Member
Column values: U2:U13
Matrix values: X16:AI27
Output cells: V2:V13
Formula: =Tocol(Mmult(Transpose(U2:U13), X16:AI27)
(Below is a wrong code. Any simple way to achieve this?)
>>> use code - tags <<<
Code:
Sub CalculateMatrixProduct()
    Dim colVector As Range
    Dim matrixRange As Range
    Dim outputRange As Range
    Dim result As Variant
    Dim i As Long, j As Long

    ' Set the column vector range (U2:U13)
    Set colVector = Sheets("Sheet2").Range("U2:U13")
    ' Set the matrix range (X16:AI27)
    Set matrixRange = Sheets("Sheet2").Range("X16:AI27")
    ' Set the output range (V2:V13)
    Set outputRange = Sheets("Sheet2").Range("V2:V13")

    ' Calculate the matrix product
    result = WorksheetFunction.MMult(Application.WorksheetFunction.Transpose(colVector), matrixRange)

    ' Populate the result in the output range
    For i = 1 To UBound(result, 1)
        outputRange.Cells(i, 1).Value = result(i, 1)
    Next i
End Sub
Reg! (Ive exhausted free VBA code generator free previleges, any 100% free websites?)
 
Last edited by a moderator:
try changing:
Code:
    ' Populate the result in the output range
    For i = 1 To UBound(result, 1)
        outputRange.Cells(i, 1).Value = result(i, 1)
    Next i
to:
Code:
    ' Populate the result in the output range
        outputRange.Value = application.transpose(result)
or:
Code:
    For i = 1 To UBound(result, 1)
        outputRange.Cells(i).Value = result(i)
    Next i
You could also change the result line to:
Code:
result = Application.MMult(Application.Transpose(colVector), matrixRange)
since an error will not stop this version of the code, you can then examine result for an error to handle the error.
 
Last edited:
Back
Top