Check if a matrix is ​​a identity matrix in Matlab

I need to check if the matrix is ​​a identity matrix. I know that there is a function that checks if the matrix is ​​a diagonal matrix, i.e. isdiag . I know that I can do the following to check if matrix a is a identity matrix:

 isequal(a, eye(size(a, 1))) 

Is there a function like isdiag tha does this directly for me?

+6
source share
2 answers
 sum(sum(A - eye(size(A,1)) < epsilon)) == 0 

Subtract your identity and check for more items than epsilon.

+1
source

As others have said, you do not necessarily want to verify exact equality with the identity matrix. In addition, using eye can potentially consume unnecessary memory for large enough matrices. I would recommend using diag to get around this.

 isdiag(a) && all(abs(diag(a) - 1) < tolerance) 
0
source

All Articles