Is there faster way to apply `det` function along the third dimension?

조회 수: 6 (최근 30일)
wei zhang
wei zhang 2020년 7월 17일
댓글: wei zhang 2020년 7월 19일
I am trying to calculate the det of many 4*4 matrix. I store the data in a matrix with shape 4*4*n. (n =4000000). I am using the for loop to get the result as below. Is there any way to accelerate the progress? Like bsxfun or arrayfun, or some simillar ideas?
v = zeros(length(m),1);
for i=1:size(m,3)
v(i)=det(m(:,:,i));
end
  댓글 수: 6
Walter Roberson
Walter Roberson 2020년 7월 18일
Bruno's criticism of the precision problems and the high length of the formula for increasing n, are valid criticisms. Generally speaking, it is often the case that making code faster comes at the price of making it less accurate towards the margins. Theoretical definitions that suppose infinite precision get substituted for more nuanced checks that deal with floating-point realities, and time gets saved by not making the checks to figure out what compensation is needed for each case.
If you have some time you should look at how hypot() (finding the length of a hypotenuse) has to be implemented in practice in order to maintain accuracy. Consider sqrt(A^2 + B^2) under the circumstance that A or B is smaller than sqrt(realmin) and so squaring it might underflow to 0...
wei zhang
wei zhang 2020년 7월 19일
@Bruno Luong Thank you for introducing the formula in wiki.
@Walter Roberson.
1. I am glad to know the matlabfunction command. It is much faster than combination of subs and syms.
2. Thank you for introducing the cause of error. It seems the error is increasing fast with the size of matrix increasing. May cause terrible results.Wow.

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2020년 7월 17일
편집: Bruno Luong 2020년 7월 17일
I would note that Walter's solution that use recursive formal determinant formula might be fast but might be sensitive to umerical errors, that is how I was tough (for medium/large size matrix).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by