complement.m

来自「用matlab写的五种矩阵求逆程序」· M 代码 · 共 25 行

M
25
字号
function inverse = complement(A)
%COMPLEMENT compute the inverse of the input matrix
SIZE = size(A);
if SIZE(1) ~= SIZE(2)
    error('the matrix must be square.');
end

if det(A) == 0
    error('the matrix should be nonsingular.');
end

determinant = det(A);
inverse = zeros(SIZE(1), SIZE(2));

for i = 1:SIZE(1)
    for j = 1:SIZE(2)
        C = A;
        C(i, 1:SIZE(2)) = 0;
        C(1:SIZE(1), j) = 0;
        C(i, j) = 1;
        inverse(i, j) = det(C);
    end
end

inverse = (inverse / determinant)';

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?