# Please help me solve such problem. I know why I get this error （because it is not a square matrix）. However, the data (4*3 matrix) given by my tutor cannot be changed, how to make it run? Thank you.

1 view (last 30 days)
Tianlan Yang on 19 Mar 2021
Answered: Walter Roberson on 19 Mar 2021 here is the function:
function [L,U] = eluinv(A)
[m,n]=size(A);
[L,U] = lu(A)
LU = roundingfixer(L*U);
if (A == LU)
disp('Yes, I got a factorization')
end
if (rank(U) == rank(A))
disp('U is an echelon form of A')
else
disp('U is not an echelon form of A? What is wrong?!')
end
if (rank(A) == n)
invL = [L eye(n)];
invU = [U eye(n)];
invL = rref(invL);
invU = rref(invU);
invL = invL(:,(n+1:n*2));
invU = invU(:,(n+1:n*2));
invA = roundingfixer(invU * invL)
P = roundingfixer(inv(A))
disp('the inverse of A calculated using LU factorization is')
disp(invA)
if (P==invA)
disp('Yes, LU factorization works for calculating the inverses')
else
disp('LU factorization does not work for me!?')
end
else
sprintf('A is not invertible')
invA = [];
end
end
roundingfixer.m:
function B = roundingfixer(A)
[m,n]=size(A);
A=closetozeroroundoff(A,7)
for i=1:m
for j=1:n
A(i,j) = round(A(i,j),8);
end
end
B=A;
end

Walter Roberson on 19 Mar 2021
You need to decide whether to use eye(m) or eye(m,n). The choice will affect
P = roundingfixer(inv(A))
That is going to fail. Your A is not square, so inv(A) is going to fail.