【发布时间】:2013-11-13 12:39:27
【问题描述】:
我正在尝试在 Python 中实现高斯消除的旋转并面临一些问题。
def pivot2(matrix,i):
# matrix is a N*N matrix
# i is the column I want to start with
m = matrix.shape[1]
for n in range(i,m):
colMax = np.argmax(abs(matrix[n:,i]), axis=0) #rowindex of highest absolute value in column
if(colMax == 0): #if max in column is in first row, stop
break;
tmpRow = copy.copy(matrix[n,:]) #create new object of same row
matrix[n,:] = matrix[colMax,:] #overwrite first row with row of max value
matrix[colMax,:] = tmpRow #overwrite old row of max value
return matrix
代码适用于i=0 就好了。但是对于i=1,我无法在整个列中搜索最大值的索引,因为它显然总是0。
当我从一个 3x3 矩阵中分割这个矩阵时:
array([[ 1., 2.],
[-3., -2.]])
并使用我的argmax 函数,索引是1。但在我的原始矩阵中,同一行的索引是 2,它交换了错误的行。我该如何解决这个问题?
有没有更简单的方法来使用切片实现旋转?
【问题讨论】:
-
看起来你可以在检查 0 后将
i添加到colmax就可以了 -
感谢您的帮助,它成功了!