Inverse of a matrix
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
%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 Commenti
Risposte (2)
Walter Roberson
il 25 Set 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 Commenti
Walter Roberson
il 25 Set 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)
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
il 25 Set 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)
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
il 5 Ott 2021
Modificato: Walter Roberson
il 5 Ott 2021
3 Commenti
Walter Roberson
il 5 Ott 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.
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!