Error "Matrix dimention must agree"

1 view (last 30 days)
Tianlan Yang on 18 Mar 2021
Answered: Tianlan Yang on 18 Mar 2021 Here is the function:
function [L,U] = eluinv(A)
[~,n]=size(A);
[L,U] = lu(A);
format compact
if closetozeroroundoff(A,7) == closetozeroroundoff(L*U,7)
disp('Yes, I have got LU factorization')
end
if closetozeroroundoff(rref(U),7) == closetozeroroundoff(rref(A),7)
disp('U is an echelon form of A')
else
disp('Something is wrong')
end
if rank(A) ~= max(size(A))
sprintf('A is not invertible')
invA=[];
return
else
leye = [L eye(size(L,1))];
ueye = [U eye(size(U,1))];
invLech = rref(leye);
invUech = rref(ueye);
invL = invLech(:,(n+1):size(invLech,2));
invU = invUech(:,(n+1):size(invUech,2));
invA = invU*invL;
if isequal(closetozeroroundoff(invA-inv(A)),zeros(size(A)))
disp('Yes, LU factorization works for calculating the inverses')
else
disp('LU factorization does not work for me?!')
end
end

Cris LaPierre on 18 Mar 2021
Edited: Cris LaPierre on 18 Mar 2021
The error message is telling you what the issue is. You are trying to compare two matrices that do not have the same number of elements.
The left side of your comparison is 3x3 while the right side is 4x3. See this documentation page for more.
This is because the result of lu is two different sized matrices.
A = [1 1 4; 0 -4 0; -5 -1 -8; 2 3 -1];
[L,U] = lu(A)
L = 4×3
-0.2000 -0.2000 -0.5714 0 1.0000 0 1.0000 0 0 -0.4000 -0.6500 1.0000
U = 3×3
-5.0000 -1.0000 -8.0000 0 -4.0000 0 0 0 -4.2000

Tianlan Yang on 18 Mar 2021
Thank you. Could you please look another question I post?