伊莉討論區

標題: 請問要如何用cofactor matrix求解矩陣A的inverse (限使用 MATLAB撰寫程式) [打印本頁]

作者: ~夜神月~    時間: 2016-10-12 10:57 AM     標題: 請問要如何用cofactor matrix求解矩陣A的inverse (限使用 MATLAB撰寫程式)

A =[5 4 2 1;7 1 6 2;4 2 3 1;5 5 6 8];
不可以用inv和det及反除 我目前卡在餘因子的地方?
有位大大寫這樣
function C = cofactor(A, i, j)

% cofactor Matrix of cofactors.
%
% C = cofactor(A) returns the matrix of cofactors of A.
% If A is invertible, then inv(A) = C' / det(A).
%
% C = cofactor(A, i, j) returns the cofactor of
% row i, column j of A.

if nargin == 3
% Remove row i and column j to produce the minor.
M = A;
M(i, = [];
M(:,j) = [];
C = (-1)^(i+j) * det(M);
else
[n,n] = size(A);
for i = 1:n
for j = 1:n
C(i,j) = cofactor(A, i, j);
end
end
end
但nargin == 3 以及[n,n] = size(A); 這邊 我看不太懂 還望各位大大解答

另外這是我自己寫的
A=[5 4 2 1;7 1 6 2;4 2 3 1;5 5 6 8];
i=1:4;
j=1:4;
b=A(i,j);
M=zeros(4,4);
ans=0;
while i<4;
M=A;
M(i, = [];
M(:, j) = [];;
ans=ans+b*-1^(i+j)*det(M)
end

不知為何ans還是0
請各位幫忙解答一下 拜託 拜託

那如果有哪位大大的寫法可以PO上來給在下參考一下嗎?


作者: Phantom28    時間: 2016-11-9 04:13 PM

沒仔細看程式邏輯對不對,僅針對你提的問題說明一下:

nargin   => 輸入引數個數(number of input argument)
nargout => 輸出引數個數(number of output argument)

以你的例子C = cofactor(A, i, j) 就是3個輸入引數A,i,j,1個輸出引數C
if nargin == 3 是如果輸入引數為3 成立的話,做之後的敘述

size(A) 是判斷矩陣A(m,n)的維度:以A(3,5)矩陣為列
[m,n]=size(A)  %結果 m=3, n=5

[n,n] = size(A); %作者可能是要讓它成為方陣





歡迎光臨 伊莉討論區 (http://www.web01.eyny.com/) Powered by Discuz!