题型:综合题 题类:常考题 难易度:困难
浙江省共美联盟2018-2019学年高二下学期信息技术期末模拟联考试卷
1)对于两个形状相同的矩阵,它们的内积是每个对应位置的数字相乘之后的和;
2)对于小矩阵和大矩阵卷积运算时,以小矩阵为参考,沿着横向和纵向两个方向逐个滑动(取同样大小的矩阵),依次横向和纵向两个方向逐个滑动(取同样大小的矩阵),依次求出内积,得到结果矩阵,如图a所示。
图a
小A同学用VB程序模拟上述的矩阵卷积运算,单击“初始化小矩阵”按钮command1,随机生成小矩阵数据,单击“初始化大矩阵”按钮command2,随机生成大矩阵数据,单击“运算”按钮command3,进行矩阵卷积运算并生成结果矩阵,运行界面如图b所示:
图b
Const m = 2 ‘小矩阵行数和列数
Const n = 8 ‘大矩阵行数和列数 Dim a(1 To m * m) As Integer Dim b(1 To n * n) As Integer
Dim tempStr As String ‘输出行的临时数据 '初始化小矩阵
Private Sub Command1_Click()
Dim x As Integer ‘小矩阵当前坐标位置
Randomize
List1.Clear
For i = 1 To m
tempStr = ""
For j = 1 To m
x = (i - 1) * m + j
a(x) = Int(Rnd() * 5 + 1) ‘小矩阵[1,5]的随机数
tempStr = tempStr + Str(a(x))
Next j
List1.AddItem tempStr
Next i End Sub
‘初始化大矩阵
Private Sub Command2_Click()
Dim x As Integer ‘大矩阵当前坐标位置
Randomize
List2.Clear
For i = 1 To n
temp Str = ""
For j = 1 To n
x =
b(x) = Int(Rnd() * 10) ‘大矩阵[0,9]的随机数
tempStr = tempStr + Str(b(x))
Next j
List2.AddItem tempStr
Next i
End Sub
‘运算
Private Sub Command3_Click()
Dim productSum As Integer '内积
Dim product As Integer
Dim rowNow As Integer, colNow As Integer
List3.Clear
For i = 1 To n - m + 1 ‘处理纵向移动
tempStr = ""
For j = 1 To n - m + 1 ‘处理横向移动
productSum = 0
For k = 1 To m * m
rowNow = (k - 1) \ m ‘小矩阵值的行号
colNow = ‘小矩阵值的列号
product = a(k) * b((i - 1 + rowNow) * n + j + colNow - 1) '大小矩阵值乘积
Next k
tempStr = tempStr + Str(productSum)
Next j
List3.AddItem tempStr
Next i
End Sub
试题篮