• API
• FAQ
• Tools
• Archive
daily pastebin goal
48%
SHARE
TWEET

# Untitled

a guest Nov 22nd, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function [ det ] = determinant( matrix )
2. [r,c] = size(matrix);
3. if r ~= c % Invalid matrix
4.     disp('Error, matrix must be square');
5. end
6. if r == 1 % Base case size 1x1
7.     det = matrix(1);
8. elseif r == 2 % Base case size 2x2
9.     det = matrix(1,1) * matrix(2,2) - matrix(1,2) * matrix(2,1);
10. else
11.     det = 0;
12.     for i=1:r
13.         sub = matrix(:,2:end); % Make copy of matrix, remove first column
14.         sub(i,:) = []; % Remove row i from reduced matrix
15.         if mod(i, 2) == 0 % Even indices are subtracted
16.             det = det - matrix(i,1) * determinant(sub); % Subtract subdeterminant from summation
17.         else % Odd indices are added
18.             det = det + matrix(i,1) * determinant(sub); % Add subdeterminant to summation
19.         end
20.     end
21. end
22. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top