我想要生成对称的零对角矩阵。我的对称部分可以工作,但是当我使用numpy中的fill_diagonal作为结果时,我得到了"None“。我的密码在下面。感谢您的阅读
import numpy as np
matrix_size = int(input("Size of the matrix \n"))
random_matrix = np.random.random_integers(-4,4,size=(matrix_size,matrix_size))
symmetric_matrix = (random_matrix + random_matrix.T)/2
print(symmetric_matrix)
zero_diogonal_matrix = np.fill_diagonal(symmetric_matrix,0)
print(zero_diogonal_matrix)
发布于 2017-09-27 11:51:36
与跨python/numpy的许多其他方法一样,np.fill_diagonal()
可以就地工作。例如:Why does “return list.sort()” return None, not the list?。也就是说,它直接改变内存中的对象,而不创建新对象。这些函数的返回值是None
。因此,改变:
zero_diogonal_matrix = np.fill_diagonal(symmetric_matrix,0)
只想:
np.fill_diagonal(symmetric_matrix,0)
然后,您将看到symmetric_matrix
中反映的更改。
https://stackoverflow.com/questions/46445894
复制相似问题