Inverse of a matrix

조회 수: 4 (최근 30일)
Kojo Anim
Kojo Anim 2021년 9월 25일
댓글: Walter Roberson 2021년 10월 5일
%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

답변 (2개)

Walter Roberson
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
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)
/tmp/tp00d7251c_511e_4989_9525_172adb4e2988/RREFbyLeftMult.m
addpath(tn)
myInv([1 2 3; 4 5 -6; -7 8 -9])
Unrecognized function or variable 'FirstNonzeroRow'.

Error in RREFbyLeftMult (line 10)
i=FirstNonzeroRow(A);

Error in solution>myInv (line 13)
B = RREFbyLeftMult(A);
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
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)
FirstNonzeroRow.m RREFbyLeftMult.m
addpath(tn)
myInv([1 2 3; 4 5 -6; -7 8 -9])
Unrecognized function or variable 'BubbleSort'.

Error in RREFbyLeftMult (line 53)
B=BubbleSort(A,pivot);

Error in solution>myInv (line 16)
B = RREFbyLeftMult(A);
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
Kojo Anim 2021년 10월 5일
편집: Walter Roberson 2021년 10월 5일
Any help on what I am not getting right in this code?
function B = myInv(A)
[n]=size(A)
i=RREFbyLeftMult(A)
for i==eye(n)
if B=i
end
end
B=zeros(A)
end
end
  댓글 수: 3
Kojo Anim
Kojo Anim 2021년 10월 5일
I want B to be that inverse I am looking for in case the reduced row echelon form of A is an identity matrix
Walter Roberson
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by