Here is the function:
function [L,U] = eluinv(A)
[~,n]=size(A);
[L,U] = lu(A)
LU = roundingfixer(L*U);
if (A == LU)
disp('LU factorization is valid')
else
disp('A mistake was made somewhere.')
end
if (rank(U) == rank(A))
disp('U is an echelon form of A')
else
disp('A mistake was made somewhere.')
end
if (rank(A) == n)
% Use rref to row reduce the matrices
invL = [L eye(n)];
invU = [U eye(n)];
invL = rref(invL);
invU = rref(invU);
% Cut off the first half of the matrix
invL = invL(:,(n+1:n*2));
invU = invU(:,(n+1:n*2));
invA = roundingfixer(invU * invL)
P = roundingfixer(inv(A))
if (invA == P)
disp('LU works for calculating the inverse for A')
else
disp('Error in calculating the inverse for A')
end
else
sprintf('A is not invertible.')
invA = [];
end
end
roundingfixer.m:
function B = roundingfixer(A)
[m,n]=size(A);
A=closetozeroroundoff(A,7)
for i=1:m
for j=1:n
A(i,j) = round(A(i,j),8);
end
end
B=A;
end

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 3월 18일
편집: Cris LaPierre 2021년 3월 18일

0 개 추천

Look at line 17: invL = [L eye(n)];
You are concatenating L and eye(n) horizontally. The error message is again telling you what the problem is. See this documentation page for more.
A = [1; 2]
A = 2×1
1 2
B = [3 4 5]
B = 1×3
3 4 5
C = [A B]
Error using horzcat
Dimensions of arrays being concatenated are not consistent.

댓글 수: 8

Tianlan Yang
Tianlan Yang 2021년 3월 18일
If I want it run, how can I edit that?
Cris LaPierre
Cris LaPierre 2021년 3월 18일
Figure out why your sizes are not what you expected, and update your code accordingly.
Tianlan Yang
Tianlan Yang 2021년 3월 18일
Hi, I still cannot figure that out. Why 3*4 matrix can run but 4*3 matrix has this error.
Cris LaPierre
Cris LaPierre 2021년 3월 18일
Horizontal concatenation means the number of rows (first dimension) must be the same. This is why a 3x4 and a 3x3 can be horizontally concatenated (both have 3 rows), but a 4x3 and a 3x3 cannot (one has 4, the other has 3, so a dimension mismatch error occurs).
Tianlan Yang
Tianlan Yang 2021년 3월 18일
Ok, I got it! So that means I need to change the horizontal concatenation to something else to make it run. Could you please provide another method to operate it beyond the row limitation? Thank you!
Cris LaPierre
Cris LaPierre 2021년 3월 18일
I'm afraid I don't undertand what it is you are trying to do, so am not in a position to recommend a solution.
Tianlan Yang
Tianlan Yang 2021년 3월 18일
Hi, I mean that is there a function that can replace horizontal concatenation (can both applied to row not equivalent situation)? Thank you so much!
Cris LaPierre
Cris LaPierre 2021년 3월 18일
I am not aware of a function that will do this for you. I believe you will need to write your code in a way to either ensure the two matrices have the same number of rows.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

2021년 3월 18일

댓글:

2021년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by