Inverse of a matrix

14 views (last 30 days)
Kojo Anim
Kojo Anim on 25 Sep 2021
Commented: Walter Roberson on 5 Oct 2021
%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

Answers (2)

Walter Roberson
Walter Roberson on 25 Sep 2021
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 Comments
Walter Roberson
Walter Roberson on 25 Sep 2021
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 on 25 Sep 2021
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

Sign in to comment.


Kojo Anim
Kojo Anim on 5 Oct 2021
Edited: Walter Roberson on 5 Oct 2021
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 Comments
Kojo Anim
Kojo Anim on 5 Oct 2021
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 on 5 Oct 2021
Then you need to calculate values and assign them to B, rather than testing the value of B that is not even assigned yet.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!