Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I know I can do the following to check if a matrix a is an identity matrix:

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

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

like image 680
nbro Avatar asked Feb 28 '16 12:02

nbro


2 Answers

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. I would recommend using diag to get around that.

isdiag(a) && all(abs(diag(a) - 1) < tolerance)
like image 120
Suever Avatar answered Oct 26 '22 17:10

Suever


sum(sum(A - eye(size(A,1)) < epsilon)) == 0

Subtract by identity and check if any elements are greater than epsilon.

like image 26
Mochan Avatar answered Oct 26 '22 19:10

Mochan