用它们的逆矩阵替换矩阵的所有元素 [英] Replace all elements of a matrix by their inverses
问题描述
我有一个简单的问题,我不知道如何解决.
I've got a simple problem and I can't figure out how to solve it.
这是一个矩阵:A = np.array([[1,0,3],[0,7,9],[0,0,8]])
.
我想找到一种快速的方法,用它们的逆值替换矩阵中的所有元素,当然不包括零元素.
I want to find a quick way to replace all elements of this matrix by their inverses, excluding of course the zero elements.
由于Stackoverflow的搜索引擎,我知道如何用条件用给定值替换元素.相反,我不知道如何根据以前的元素(例如,平方元素,逆元素等)用新元素替换元素
I know, thanks to the search engine of Stackoverflow, how to replace an element by a given value with a condition. On the contrary, I do not figure out how to replace elements by new elements depending on the previous ones (e.g. squared elements, inverses, etc.)
推荐答案
还有关于Antti Haapala答案的注释,(对不起,我无法发表评论) 如果您想保留0,可以使用
And just a note on Antti Haapala's answer, (Sorry, I can't comment yet) if you wanted to keep the 0's, you could use
B=1./A #I use the 1. to make sure it uses floats
B[B==np.inf]=0
这篇关于用它们的逆矩阵替换矩阵的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!