Recursion in matrix calculation

37 Ansichten (letzte 30 Tage)
StillANovice
StillANovice am 25 Aug. 2020
Bearbeitet: James Tursa am 25 Aug. 2020
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

Antworten (1)

James Tursa
James Tursa am 25 Aug. 2020
Bearbeitet: James Tursa am 25 Aug. 2020
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.

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by