[英]Check if a matrix is an identity matrix in Matlab
I need to check if a matrix is an identity matrix. 我需要检查矩阵是否是一个单位矩阵。 I know there's a function which check if a matrix is a diagonal matrix, that is,
isdiag
. 我知道有一个函数可以检查矩阵是否是对角矩阵,即
isdiag
。 I know I can do the following to check if a matrix a
is an identity matrix: 我知道我可以执行以下操作来检查矩阵
a
是否是单位矩阵:
isequal(a, eye(size(a, 1)))
Is there a function like isdiag
tha does it directly for me? 有没有像
isdiag
这样的函数直接为我做的?
sum(sum(A - eye(size(A,1)) < epsilon)) == 0
按身份减去并检查是否有任何元素大于epsilon。
As others have said, you don't necessarily want to check for exact equality to the identity matrix. 正如其他人所说,您不一定要检查身份矩阵的确切相等性。 Also using
eye
can potentially take up an unnecessary amount of memory for sufficiently large matrices. 同样使用
eye
可能会为足够大的矩阵占用不必要的内存量。 I would recommend using diag
to get around that. 我建议使用
diag
来解决这个问题。
isdiag(a) && all(abs(diag(a) - 1) < tolerance)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.