Recursion in matrix calculation

조회 수: 8 (최근 30일)
StillANovice
StillANovice 2020년 8월 25일
편집: James Tursa 2020년 8월 25일
Hi,
How does recursion work in this context, as in how did the function CalDet manage to calculate the determinant of the minor without explicitly writing an equation?
function [determinant] = CalDet(M)
dimensionM = size(M);
if (dimensionM(1) == 1)
determinant = M(1, 1);
else
determinant = 0;
for i = 1:dimensionM(2)
determinant = determinant + (-1)^(i+1) * M(1, i) * CalDet(MMin(M, 1, i));
end
end
end
function [MatrixMinor] = MMin(M, i, j)
dimensionM = size(M);
MatrixMinor = M([1:(i-1) (i+1):dimensionM(1)], [1:(j-1) (j+1):dimensionM(2)]);
end

답변 (1개)

James Tursa
James Tursa 2020년 8월 25일
편집: James Tursa 2020년 8월 25일
This uses recursive calls (CalDet calls CalDet with smaller matrices until the size is 1x1). I.e., the recursion continues all the way down until the input is a 1x1 matrix, at which point the result is simply M(1,1) and then the results get passed back up through the stack of calls.
See Laplace's expansion and the adjugate matrix here:
BTW, this is not a good numerical technique.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by