Error "Matrix dimention must agree"
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Cris LaPierre
2021-3-18
编辑:Cris LaPierre
2021-3-18
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)
0 个评论
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!