필터 지우기
필터 지우기

Error "Matrix dimention must agree"

조회 수: 1 (최근 30일)
Tianlan Yang
Tianlan Yang 2021년 3월 18일
답변: Tianlan Yang 2021년 3월 18일
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
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)
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

추가 답변 (1개)

Tianlan Yang
Tianlan Yang 2021년 3월 18일
Thank you. Could you please look another question I post?

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by