MATLAB: How to compare each individual column vectors of a matrix to a set of vectors

matrix identity-matrix

What i basically need is to find if the system of equations that i have, already has a identity matrix or atleast part of it. i.e. if i have a matrix
1 2 1 0;
2 3 0 1;
3 4 0 0;
Though there is no 3×3 identity matrix in the above matrix it still contains 2/3 vectors that make one.
So how to code this?
Really appreciate the help. I am completely new to matlab.
Regards

Best Answer

Here is my best guess at some core code that might do what you want.
A = [1 2 1 0;
2 3 0 1;
3 4 0 0];
[m,n] = size(A);
for m1 = 1:m-1
for m2 = m1+1:m
for n1 = 1:n-1
for n2 = n1+1:n
T = A(m1:m2,n1:n2);
[tm,tn] = size(T);
if tm==tn && isequal(T,eye(tm))
sprintf('Identity matrix at A(%d:%d,%d:%d)',m1,m2,n1,n2)
end
end
end
end
end
It is a very straightforward implementation, and I do not take advantage of even simple methods to make it more efficient. (For example, the loops run over non-square submatrices of A.) But I hope you get the idea, and perhaps can tailor it to what you need.