Error "Matrix dimensions must agree" in the line "T = norm(rref(Y)- rref(U)) < tol;" I used "A=[1 1 4;0 -4 0;-5 -1 -8; 2 3 -1] [L,U] = eluinv(A);" in my livescript. Thank you so much!

1 回表示 (過去 30 日間)
Here is the function.m:
function [L,U] = eluinv(A)
[m,n] = size(A);
[L,U] = lu(A);
B = L*U;
C = closetozeroroundoff(B,7);
q = closetozeroroundoff(A,7);
if q == C
disp('Yes, I have got LU factorization');
end
Y = closetozeroroundoff(A,7);
tol = 1e-6;
T = norm(rref(Y)- rref(U)) < tol;
if T
disp('U is an echelon form of A');
else
disp('Something is wrong')
end
if rank(A) ~= n
sprintf('A is not invertible');
invA = [];
return
else
invL = eye(n)/rref(L);
invU = eye(n)/rref(U);
invA = invU * invL;
end
P = inv(A);
J = closetozeroroundoff(P,7);
Q = closetozeroroundoff(invA,7);
if J == Q
disp('Yes, LU factorization works for calculating the inverses');
else
disp('LU factorization doesnt work for me');
end
  4 件のコメント
Tianlan Yang
Tianlan Yang 2021 年 3 月 18 日
Hi , here is the closetozeroroundoff function:
function B=closetozeroroundoff(H,p)
H(abs(H)<10^-p)=0;
B=H;
end
Tianlan Yang
Tianlan Yang 2021 年 3 月 18 日
編集済み: Tianlan Yang 2021 年 3 月 18 日
Could you please show how to make it work? Because I try 3*4 matrix, it works, but when I try 4*3 matrix, it shows error. Thank you!

サインインしてコメントする。

採用された回答

KSSV
KSSV 2021 年 3 月 18 日
Replace the line:
T = norm(rref(Y)- rref(U)) < tol;
with
T = (norm(rref(Y))- norm(rref(U))) < tol;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by