Well, first of all, you actually NEVER want to calculate the determinants, you just think that it is. In fact, this is almost never good, because determinants scale so poorly. Too often they are used to deduce the state of the singularity of the matrix, which is very difficult to do from the point of view of numerical analysis.
Outlining my mini pomp against determinants in general ...
OPTION 1:
3- , . mat2cell .
, cellfun . cellfun (@det) , . ? , det , , .
2:
, , , 2x2 3x3, . , , , 2x2, Y 2x2xn:
d = Y(1,1,:).*Y(2,2,:) - Y(1,2,:).*Y(2,1,:);
, , 2x2 Y. 3x3 , , . 3x3- , .
d = Y(1,1,:).*Y(2,2,:).*Y(3,3,:) + ...
Y(2,1,:).*Y(3,2,:).*Y(1,3,:) + ...
Y(3,1,:).*Y(1,2,:).*Y(2,3,:) - ...
Y(3,1,:).*Y(2,2,:).*Y(1,3,:) - ...
Y(2,1,:).*Y(1,2,:).*Y(3,3,:) - ...
Y(1,1,:).*Y(3,2,:).*Y(2,3,:);
, OPTION 2 , .
: , . , 1e5-.
p = 2;
n = 1e5;
Y = rand(p,p,n);
tic,
d0 = squeeze(Y(1,1,:).*Y(2,2,:) - Y(2,1,:).*Y(1,2,:));
toc
Elapsed time is 0.002141 seconds.
tic,
X = squeeze(mat2cell(Y,p,p,ones(1,n)));
d1= cellfun(@det,X);
toc
Elapsed time is 12.041883 seconds.
.
std(d0-d1)
ans =
3.8312e-17
, , , . , MANY , 2x2 3x3. 44 . , , , .
, MATLAB det LU, . , , 2x2 3x3 - . ( , , .)