Matrix summation rounding error?

조회 수: 6 (최근 30일)
Giuseppe Gallo
Giuseppe Gallo 2019년 5월 23일
편집: Stephen23 2025년 2월 7일
Dear all,
I'm having trouble with summation of matrices and difference of the two sum. Here some lines of code:
thetaMN_prev = cellfun(@nansum, theta_prev);
cost_prev = sum(thetaMN_prev,'all');
thetaMN_try = cellfun(@nansum, theta_try);
cost_try = sum(thetaMN_try,'all');
cost_prev - cost_try often returns 0, while
sum(thetaMN_try - thetaMN_prev,'all') ~= 0, but this should be the same calculation mathematically speaking.
When this happens, cost_try and cost_prev are of the order of 1e19.
Is this an error due to some rounding process in function sum(A,'all')?
  댓글 수: 1
Stephen23
Stephen23 2025년 2월 7일
편집: Stephen23 2025년 2월 7일
"... this should be the same calculation mathematically speaking."
Sure, but computing with binary floating point numbers is not the same as mathematics that you learned at school.
In particular, floating point addition is not associative:

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

답변 (1개)

Harshavardhan
Harshavardhan 2025년 2월 7일
The issue you're experiencing is likely due to floating-point precision errors. When dealing with very large numbers (e.g., 1e19), small differences can be lost due to rounding errors inherent in floating-point arithmetic. This can occur when subtracting two nearly equal large numbers, leading to a loss of precision.
We see this difference in results due to the order of operations:
  • Summing First, Then Subtracting:
cost_prev - cost_try
  • Here you're summing all elements of "thetaMN_prev" and "thetaMN_try" separately and then subtracting the two sums. This approach can amplify precision errors, because the subtraction of two large numbers can lead to significant cancellation error.
  • Subtracting Element-wise, Then Summing:
sum(thetaMN_try - thetaMN_prev, 'all')
  • Here you're subtracting corresponding elements of "thetaMN_try" and "thetaMN_prev first", and then summing the resulting differences. This method can be more accurate because it avoids the large intermediate sums and directly computes the overall difference, reducing the impact of floating-point precision errors.
I suggest using the second approach as its generally more accurate and can help minimize precision errors.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by