替换 Python 中给定 k 的矩阵的所有下对角线
Replace all the subdiagonals of a matrix for a given k in Python
我想替换 k 对角线下所有次对角线的值。
例如:
我们首先导入numpy库:
import numpy as np
然后我们创建矩阵:
In [14]: matrix = np.matrix('1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1')
然后我们得到:
In [15]: print(matrix)
Out[16]:
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
然后我们得到 k = 1 的 k 对角线下的对角线,例如:
In [17]: lowerdiags = [np.diag(matrix, k=e+1).tolist() for e in range(-len(matrix), k)]
In [18]: print(lowerdiags)
Out[19]: [[1], [1, 1], [1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]
而且,我被困在那里,我应该添加什么让它成为 k = 1 并替换所有值 0,就像这样:(知道我们刚刚找到了次对角线)
[[0 1 1 1 1 1]
[0 0 1 1 1 1]
[0 0 0 1 1 1]
[0 0 0 0 1 1]
[0 0 0 0 0 1]]
甚至对于 k = 0 :
[[1 1 1 1 1 1]
[0 1 1 1 1 1]
[0 0 1 1 1 1]
[0 0 0 1 1 1]
[0 0 0 0 1 1]]
感谢您的帮助和耐心等待。
我找到了一种使用 numpy 方法的方法:fill_diagonal 并在不同的 k 之间移动:
# Import numpy library
import numpy as np
def Exercise_3(matrix, k):
# print initial matrix
print(matrix)
for k in range(-len(matrix)+1, k):
if k < 0:
# Smart slicing when filling diagonals with "np.fill_diagonal" on our matrix for lower diagonals
np.fill_diagonal(matrix[-k:, :k], 0)
if k > 0:
# Smart slicing when filling diagonals with "np.fill_diagonal" on our matrix for upper diagonals
np.fill_diagonal(matrix[:-k, k:], 0)
if k == 0:
# Just replace the main diagonal by 0
np.fill_diagonal(matrix, 0)
# print to see each change on the matrix
#print(matrix)
#print(k)
return matrix
def main():
k = 0
# an another way of creating a matrix
#matrix = np.matrix('1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1')
# matrix of 5 rows and 5 columns filled by 1
matrix = np.array(([1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]))
NewMatrix = Exercise_3(matrix, k)
print(NewMatrix)
main()
我想替换 k 对角线下所有次对角线的值。
例如:
我们首先导入numpy库:
import numpy as np
然后我们创建矩阵:
In [14]: matrix = np.matrix('1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1')
然后我们得到:
In [15]: print(matrix)
Out[16]:
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
然后我们得到 k = 1 的 k 对角线下的对角线,例如:
In [17]: lowerdiags = [np.diag(matrix, k=e+1).tolist() for e in range(-len(matrix), k)]
In [18]: print(lowerdiags)
Out[19]: [[1], [1, 1], [1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]
而且,我被困在那里,我应该添加什么让它成为 k = 1 并替换所有值 0,就像这样:(知道我们刚刚找到了次对角线)
[[0 1 1 1 1 1]
[0 0 1 1 1 1]
[0 0 0 1 1 1]
[0 0 0 0 1 1]
[0 0 0 0 0 1]]
甚至对于 k = 0 :
[[1 1 1 1 1 1]
[0 1 1 1 1 1]
[0 0 1 1 1 1]
[0 0 0 1 1 1]
[0 0 0 0 1 1]]
感谢您的帮助和耐心等待。
我找到了一种使用 numpy 方法的方法:fill_diagonal 并在不同的 k 之间移动:
# Import numpy library
import numpy as np
def Exercise_3(matrix, k):
# print initial matrix
print(matrix)
for k in range(-len(matrix)+1, k):
if k < 0:
# Smart slicing when filling diagonals with "np.fill_diagonal" on our matrix for lower diagonals
np.fill_diagonal(matrix[-k:, :k], 0)
if k > 0:
# Smart slicing when filling diagonals with "np.fill_diagonal" on our matrix for upper diagonals
np.fill_diagonal(matrix[:-k, k:], 0)
if k == 0:
# Just replace the main diagonal by 0
np.fill_diagonal(matrix, 0)
# print to see each change on the matrix
#print(matrix)
#print(k)
return matrix
def main():
k = 0
# an another way of creating a matrix
#matrix = np.matrix('1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1; 1 1 1 1 1 1')
# matrix of 5 rows and 5 columns filled by 1
matrix = np.array(([1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]))
NewMatrix = Exercise_3(matrix, k)
print(NewMatrix)
main()