VBA中的矩阵乘法并将值存储在另一个矩阵中

时间:2018-04-05 09:05:39

标签: arrays vba matrix

我坚持使用纯矩阵乘法。我有两个矩阵:ArrW和ArrC。每个元素由4个元素组成。结果应保存在第一个插槽的第三个矩阵中:ArrWs(1)。然后我改变ArrW中的数字并将该矩阵与相同的ArrC相乘。结果应存储在ArrWd(2)中。

在调试期间,行ArrWs(x) = Application.WorksheetFunction.MMult(ArrC, ArrW)会导致错误。我不知道为什么。我检查了矩阵ArrW和ArrC的实现情况。每个人都有4个号码 - 按计划。

Sub Matrix()

Dim TabelaEK As Range
Dim Size As Integer
Dim x As Integer
Dim y As Integer
Dim ArrW As Variant
Dim ArrWs As Variant
Dim ArrC As Variant

Set TabelaEK = ActiveSheet.ListObjects("ek").Range.Cells(1, 1)
Size = Range("E2").Value

ReDim ArrW(1 To Size)
ReDim ArrC(1 To Size)
ReDim ArrWs(1 To Size)

For x = 1 To Size 'Size is 4
    ArrW(x) = Cells(TabelaEK.Row + x, TabelaEK.Column + Size + 1)
Next
        'MsgBox ArrW(1)
        'MsgBox ArrW(2)
        'MsgBox ArrW(3)
        'MsgBox ArrW(4)

For x = 1 To Size
    For y = 1 To Size
        ArrC(y) = Cells(TabelaEK.Row + x, TabelaEK.Column - 1 + y)
    Next
        'MsgBox ArrC(1)
        'MsgBox ArrC(2)
        'MsgBox ArrC(3)
        'MsgBox ArrC(4)

    ArrWs(x) = Application.WorksheetFunction.MMult(ArrC, ArrW) 'here is the error!

Next
    'MsgBox ArrWs(1)
    'MsgBox ArrWs(2)
    'MsgBox ArrWs(3)
    'MsgBox ArrWs(4)
End Sub

1 个答案:

答案 0 :(得分:1)

MMULT要求array1中的列数与array2中的行数相匹配。所以使用一维数组你会说1 = UBound(ArrW) 如果有多行,那么它应该失败。

MMULT

  

Array1中的列数必须与行数相同   在Array2中,两个数组都必须只包含数字。