Sheet3 是输入(矩阵 M)表,Sheet2 是输出(结束矩阵 M)表。
Private Sub ReMatrixM()
Dim arrInput() As Variant
Dim arrSumOfMatrixAs(592, 592) As Long
Dim Ones() As Integer 'An array keeping indexes of columns which iclude 1.
Dim iOnes As Integer
Dim RowM As Integer, ColM As Integer, iRowA As Integer, iColA As Integer
Dim shtM As Worksheet, shtM2 As Worksheet, cell As Variant
'1. Take the values included in the sheet in an array
Set shtM = Worksheets("Sheet3")
arrInput = shtM.Range("B2").Resize(1066, 592)
'2. We find columns which includes 1s
'3. We will use this column indexes by binary combinations to fint the coordinates where 1s are to be added.
'Now we cycle all rows of array
For RowM = 1 To 1066 'Rows
ReDim Ones(0)
iOnes = 0
'Now we cycle all colums for each row of array
For ColM = 1 To 592 'Columns
If arrInput(RowM, ColM) = 1 Then
iOnes = iOnes + 1
ReDim Preserve Ones(iOnes)
Ones(iOnes) = ColM 'We are taking indexes of columns which includes one.
Else
arrInput(RowM, ColM) = 0
End If
Next
If UBound(Ones) > 0 Then
'For every row of arrInput add the values say cells of Matrix A (arrSumOfMatrixAs).
For iRowA = 1 To UBound(Ones)
For iColA = 1 To UBound(Ones)
arrSumOfMatrixAs(Ones(iRowA), Ones(iColA)) = arrSumOfMatrixAs(Ones(iRowA), Ones(iColA)) + 1
Next
Next
End If
Next
'Than we add the sum of "matrix A"s to arrInput
For RowM = 1 To 592
For ColM = 1 To 592
arrInput(RowM, ColM) = arrInput(RowM, ColM) + arrSumOfMatrixAs(RowM, ColM)
Next
Next
Set shtM2 = Worksheets("Sheet2")
'We reflect the arrInput to the sheet (Matrix M) at the end.
shtM2.Range("B2").Resize(1066, 592) = arrInput
End Sub
第一个代码首先将所有矩阵 A 的值相加,然后将它们添加到矩阵 M。但是如果您想逐行执行此操作,我的意思是如果在应用前一个矩阵 A 之后计算下一个矩阵 A,这是代码:
Private Sub ReMatrixM2()
Dim arrInput() As Variant
Dim Ones() As Integer 'An array keeping indexes of columns which iclude 1.
Dim iOnes As Integer
Dim RowM As Integer, ColM As Integer, iRowA As Integer, iColA As Integer
Dim shtM As Worksheet, shtM2 As Worksheet, cell As Variant
'1. Take the values included in the sheet in an array
Set shtM = Worksheets("Sheet3")
arrInput = shtM.Range("B2").Resize(1066, 592)
'2. We find columns which includes 1s
'3. We will use this column indexes by binary combinations to fint the coordinates where 1s are to be added.
'Now we cycle all rows of array
For RowM = 1 To 1066 'Rows
ReDim Ones(0)
iOnes = 0
'Now we cycle all colums for each row of array
For ColM = 1 To 592 'Columns
If arrInput(RowM, ColM) > 0 Then 'See the difference
iOnes = iOnes + 1
ReDim Preserve Ones(iOnes)
Ones(iOnes) = ColM 'We are taking indexes of columns which includes one.
Else
arrInput(RowM, ColM) = 0
End If
Next
If UBound(Ones) > 0 Then
'For every row of arrInput add the values in -say- cells of Matrix A to arrInput.
For iRowA = 1 To UBound(Ones)
For iColA = 1 To UBound(Ones)
arrInput(Ones(iRowA), Ones(iColA)) = arrInput(Ones(iRowA), Ones(iColA)) + 1
Next
Next
End If
Next
Set shtM2 = Worksheets("Sheet2")
'We reflect the arrInput to the sheet (Matrix M) at the end.
shtM2.Range("B2").Resize(1066, 592) = arrInput
End Sub