フィルターのクリア

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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by