Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. (40 points) Write a VBA code to perform matrix multiplication operation: B ar

ID: 3281625 • Letter: 3

Question

3. (40 points) Write a VBA code to perform matrix multiplication operation: B are given matrices in spreadsheet. Then use your code to calculate matrix multiplication: 1 -2 3 4 4 -3 2 1 -1 2 3 1 2 2 -1 211 1 2 1 2 -2 3-413x413 2 1-2 214XS Specific requirements: 1) Select and read matrices [A] and [B] from spreadsheet; 2) Check if the number of columns in matrix [A] matches with the number of rows in matrix [B]; if not match, please prompt a message "The size of input matrices doesn't match!" and stop the calculation; 3) Return result matrix [C] to spreadsheet; 4) you are not allowed to use "Application. WorkSheetFunction.MMult" in your code Hint: you can use "UBound(A,2)" to find the number of columns in matrix [A].

Explanation / Answer

Function Multiply(ParamArray IPM() As Variant) As Variant
Dim i As Long, j As Long, k As Long, L As Long, temp As Double
Dim M As Variant, Matrix As Variant, hold As Variant

'***********************************************
'* Function multiplies n matrices.             *
'* Matrices must be conformable:               *
'* Matrix M(i) is mxn and matrix M(i+1) is nxm *
'***********************************************

    'Cycle through the parameter array multiplying each matrix
    M = IPM(0)
    For L = LBound(IPM) To UBound(IPM) - 1
  
        'Check that matrices are comformable
        If UBound(M, 2) <> UBound(IPM(L + 1), 1) Then Exit Function
      
        'Redimension the product matrix
        ReDim Matrix(1 To UBound(M, 1), 1 To UBound(IPM(L + 1), 2))
      
  
      'Multiplication routine
        For i = 1 To UBound(M, 1)
            For j = 1 To UBound(IPM(L + 1), 2)
                For k = 1 To UBound(IPM(L + 1), 1)
                    temp = temp + M(i, k) * IPM(L + 1)(k, j)
                Next k
                Matrix(i, j) = temp
                temp = 0
            Next j
        Next i
      
        M = Matrix
    Next L
    M_Mult = Matrix
End Function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote