有高手解释矩阵的算法么?要全覆盖设计

longcn 2003-05-14 12:57:17
例如:15个数字(1-15) 每6个1组 保证每5个号码同时在“某”1组 中出现 1次
...全文
172 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
leswang107 2003-05-14
买彩票吗?15选6?晕~~~~~
回复
道素 2003-05-14
具体算法我不知道,我提供一些矩阵的基本算法
一.矩阵的加、减法:

矩阵的加法和减法就是将两个矩阵相对应的元素相加,因此两个矩阵若要进行加、减法,则两者必须具有相同的列数和相同的行数,若两个矩阵的列数或行数不同,则无法进行加、减法。

Public Sub MatrixAddition(m() As Single, n() As Single, ReturnValue() As Single)
Dim i As Long, j As Long, row As Long, column As Long
row = UBound(m, 1)
column = UBound(m, 2)
ReDim ReturnValue(1 To row, 1 To column)
For i = 1 To row
For j = 1 To column
ReturnValue(i, j) = m(i, j) + n(i, j)
Next
Next
End Sub
Public Sub MatrixSubtraction(m() As Single, n() As Single, ReturnValue() As Single)
Dim i As Long, j As Long, row As Long, column As Long
row = UBound(m, 1)
column = UBound(m, 2)
ReDim ReturnValue(1 To row, 1 To column)
For i = 1 To row
For j = 1 To column
ReturnValue(i, j) = m(i, j) - n(i, j)
Next
Next
End Sub
注意:以上两个副程式并没有判断两个矩阵是否具有相同的列数和相同的行数。

二.矩阵的乘法:

矩阵的乘法并非一般代数乘法,若两个矩阵A、B的乘积AB=C,则

C 的 i , j 元素 = (A的 i 列)·(B的 j 行)

因此若要使得 C 有意义,若 A 为一 n×r 矩阵,则B一定是 r×m 矩阵,也就是说 A 的行数一定要等于B的列数,才可进行矩阵乘法,如此将产生一个 n×m 的矩阵;但是反过来不一定成立,也就是说若 AB 相乘有意义,但是 BA 相乘不一定有意义;如果AB与BA皆有意义,也不表示 AB=BA,亦即矩阵的乘法无交换性。矩阵的乘法之所以要如此定义,主要是为了方便描述一复杂的线性方程式系统。

Public Sub MatrixMultiply(m() As Single, n() As Single, ReturnValue() As Single)
Dim i As Long, j As Long, k As Long, row As Long, column As Long, max As Long
row = UBound(m, 1)
column = UBound(n, 2)
max = UBound(m, 2)
ReDim ReturnValue(1 To row, 1 To column)
For i = 1 To row
For j = 1 To column
For k = 1 To max
ReturnValue(i, j) = ReturnValue(i, j) + m(i, k) * n(k, j)
Next
Next
Next
End Sub
注意:以上的副程式并没有判断 A 的行数是否等于 B 的列数。

三.矩阵的转置(transpose):

将一矩阵的列元素和行元素互相对调,即为其转置矩阵,因此一 n×m 的矩阵其转置矩阵为m×n。

Public Sub MatrixTranspose(m() As Single, ReturnValue() As Single)
Dim i As Long, j As Long, row As Long, column As Long
row = UBound(m, 1)
column = UBound(m, 2)
ReDim ReturnValue(1 To column, 1 To row)
For i = 1 To row
For j = 1 To column
ReturnValue(j, i) = m(i, j)
Next
Next
End Sub
四.判断一矩阵是否为对称矩阵:

若一矩阵 A 与其转置后的矩阵 At 相等,则 A 称为对称矩阵。若 A 为 n×m,则 At 必定为m×n,因此 A=At 的必要条件为 m=n,所以对称矩阵一定是方阵(square matrix)。

Public Function IsSymmetric(m() As Single) As Boolean
Dim i As Long, j As Long
For i = 1 To UBound(m, 1)
For j = 1 To UBound(m, 1)
If m(j, i) <> m(i, j) Then Exit Function
Next
Next
IsSymmetric = True
End Function
注意:以上的副程式并没有判断该矩阵是否为方阵。
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7490

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2003-05-14 12:57
社区公告
暂无公告