七、矩阵(二维数组)运算
(1)矩阵的加、减运算
C(i,j)=a(i,j)+b(i,j) 加法
C(i,j)=a(i,j)-b(i,j) 减法
(2)矩阵相乘
(矩阵A有M*L个元素,矩阵B有L*N个元素,则矩
阵C=A*B有M*N个元素)。矩阵C中任一元素 c(i,j) (a(i,k) b(k,j))
k 1l (i=1,2,…,m; j=1,2,…,n)
For i = 0 To m
For j = 0 To n
c(i, j) = 0
For k = 0 To l
c(i, j) = c(i, j) + a(i, k) * b(k, j)
Next k
Next j
Next i
(3)矩阵传置
例:有二维数组a(5,5),要对它实现转置,可用下面两种方式: For i=1 to 5 (2) For i=2 to 5
For j=i+1 to 5 For j=1 to i
t=a(i,j) t=a(i,j)
a(i,j)= a(j,i) a(i,j)= a(j,i)
a(j,i)=t a(j,i)=t
Next j Next j
Next i Next i
(4)求二维数组中最小元素及其所在的行和列 基本思路同一维数组,可用下面程序段实现(以二维数组a(2,3)为例):
‘变量max中存放最大值,row,column存放最大值所在行列号
Max = a(1, 1): row = 1: Column = 1
For i = 1 To 2