【发布时间】:2023-02-11 01:54:30
【问题描述】:
Function ReduceToRREF(matrixRange As Range) As Variant
Dim matrix As Variant
Dim rowCount As Long
Dim colCount As Long
Dim lead As Long
Dim r As Long
Dim c As Long
Dim i As Long
Dim multiplier As Double
matrix = matrixRange.Value
rowCount = UBound(matrix, 1)
colCount = UBound(matrix, 2)
lead = 1
For r = 1 To rowCount
If colCount < lead Then Exit For
i = r
While matrix(i, lead) = 0
i = i + 1
If rowCount < i Then
i = r
lead = lead + 1
If colCount < lead Then Exit For
End If
Wend
If i <> r Then
For c = lead To colCount
matrix(r, c) = matrix(r, c) + matrix(i, c)
Next c
End If
multiplier = matrix(r, lead)
For c = lead To colCount
matrix(r, c) = matrix(r, c) / multiplier
Next c
For i = 1 To rowCount
If i <> r Then
multiplier = matrix(i, lead)
For c = lead To colCount
matrix(i, c) = matrix(i, c) - multiplier * matrix(r, c)
Next c
End If
Next i
lead = lead + 1
Next r
ReduceToRREF = matrix
End Function
我认为这是一个很好的解决方案,而且在大多数情况下它似乎都能正常工作。但是,我遇到了一个失败的例子:
这:
关于可能出问题的任何想法?
我还尝试仅采用矩阵前三行的 RREF,并且按预期工作。这是怎么回事?
【问题讨论】:
-
如果您编辑问题并尝试解释用文字你尝试完成什么,我的意思是要应用的算法,你可能会得到一些帮助。否则,很难猜测一个运行不佳的代码必须做什么来对抗它......
-
你真的需要帮助吗?
标签: excel vba linear-algebra