帮我检查一下这个代码的错误 Function Convolution(rng1 As Range, rng2 As Range) As Variant '获取输入区域的大小 Dim rows1 As Integer, cols1 As Integer Dim rows2 As Integer, cols2 As Integer rows1 = rng1.Rows.Count cols1 = rng1.Columns.Count rows2 = rng2.Rows.Count cols2 = rng2.Columns.Count '创建输出数组 Dim output() As Double ReDim output(1 To rows1 + rows2 - 1) '计算卷积 Dim i As Integer, j As Integer, k As Integer For i = 1 To rows1 + rows2 - 1 output(i, 1) = 1 For j = 1 To cols1 For k = 1 To cols2 If i - k + 1 > 0 And i - k + 1 <= rows1 Then output(i, 1) = output(i, 1) + rng1(i - k + 1, j).Value * rng2(rows2 - k + 2 - (i - 1), j).Value End If Next k Next j Next i '将结果输出到表格中 Convolution = output End Function
时间: 2023-12-23 07:04:47 浏览: 120
Keras Convolution1D与Convolution2D区别说明
在这段代码中,存在以下几个错误:
1. 在创建输出数组时,未给出数组的第二个维度,应该为 `ReDim output(1 To rows1 + rows2 - 1, 1)`。
2. 在计算卷积时,对于 `output` 数组的赋值应该是 `output(i, 1) = 0`,而不是 `output(i, 1) = 1`。
3. 在计算卷积时,对于第二个 `For` 循环的范围应该是 `1 To cols2`,而不是 `1 To cols1`。
4. 在计算卷积时,对于第三个 `For` 循环的范围应该是 `1 To rows2`,而不是 `1 To rows1`。
下面是修改后的代码:
```
Function Convolution(rng1 As Range, rng2 As Range) As Variant
'获取输入区域的大小
Dim rows1 As Integer, cols1 As Integer
Dim rows2 As Integer, cols2 As Integer
rows1 = rng1.Rows.Count
cols1 = rng1.Columns.Count
rows2 = rng2.Rows.Count
cols2 = rng2.Columns.Count
'创建输出数组
Dim output() As Double
ReDim output(1 To rows1 + rows2 - 1, 1)
'计算卷积
Dim i As Integer, j As Integer, k As Integer
For i = 1 To rows1 + rows2 - 1
output(i, 1) = 0
For j = 1 To cols1
For k = 1 To cols2
If i - k + 1 > 0 And i - k + 1 <= rows2 Then
output(i, 1) = output(i, 1) + rng1(i - k + 1, j).Value * rng2(rows2 - k + 2 - (i - 1), j).Value
End If
Next k
Next j
Next i
'将结果输出到表格中
Convolution = output
End Function
```
阅读全文