Inverse of a matrix
조회 수: 4 (최근 30일)
이전 댓글 표시
%Produce a matlab function that takes n x n A matrix and produces an n x n
%matrix B such that AB=BA=I. If A is not invertible, then your function
%should return an empty(0x0) matrix. Name your function "myInv.m"
I am trying to find B, a matrix multiplied on the left of A such that BA=rref(A). I am using RREFbyLeftMult(A) that has been created for us already to get B.
I then check A*B and B*A to see if they all equal to the n*n identity matrix else I return B=zeros(n,n).
My code does not run. Please help me out as I am still learning.
function B =myInv(A)
[n,n]=size(A);
if n==n
B = RREFbyLeftMult(A);
p=B*A;
q=A*B;
disp(B);
p==eye(n,n) %check if p is an identity matrix
q==eye(n,n)
B=RREFbyLeftMult(A);
if A=eye(n,n)
B=zeros(n,n);
end
end
댓글 수: 0
답변 (2개)
Walter Roberson
2021년 9월 25일
if n==n
Under what circumstances could that be false?
p==eye(n,n) %check if p is an identity matrix
You are assuming there is no floating point round-off.
B = RREFbyLeftMult(A);
B=RREFbyLeftMult(A);
Why are you doing that again? Has A changed? Has the function changed? Does the function use random calculations?
if A=eye(n,n)
Comparisons require == not =
댓글 수: 5
Walter Roberson
2021년 9월 25일
tn = tempname();
mkdir(tn);
tf = fullfile(tn, 'RREFbyLeftMult.m');
urlwrite('https://www.mathworks.com/matlabcentral/answers/uploaded_files/749449/RREFbyLeftMult.m', tf);
ls(tf)
addpath(tn)
myInv([1 2 3; 4 5 -6; -7 8 -9])
function B =myInv(A)
[m,n]=size(A);
if m==n
B = RREFbyLeftMult(A);
p=B
q=A*B;
disp(B);
p==eye(m,n) %check if p is an identity matrix
q==eye(m,n)
else
B=zeros(m,n);
end
end
Walter Roberson
2021년 9월 25일
tn = tempname();
mkdir(tn);
tf = fullfile(tn, 'RREFbyLeftMult.m');
urlwrite('https://www.mathworks.com/matlabcentral/answers/uploaded_files/749449/RREFbyLeftMult.m', tf);
tf = fullfile(tn, 'FirstNonzeroRow.m');
urlwrite('https://www.mathworks.com/matlabcentral/answers/uploaded_files/749459/FirstNonzeroRow.m', tf);
ls(tn)
addpath(tn)
myInv([1 2 3; 4 5 -6; -7 8 -9])
function B =myInv(A)
[m,n]=size(A);
if m==n
B = RREFbyLeftMult(A);
p=B
q=A*B;
disp(B);
p==eye(m,n) %check if p is an identity matrix
q==eye(m,n)
else
B=zeros(m,n);
end
end
Kojo Anim
2021년 10월 5일
편집: Walter Roberson
2021년 10월 5일
댓글 수: 3
Walter Roberson
2021년 10월 5일
Then you need to calculate values and assign them to B, rather than testing the value of B that is not even assigned yet.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!