Recursion in matrix calculation
조회 수: 9 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!