필터 지우기
필터 지우기

Why different result when sum matrix by different order

조회 수: 1 (최근 30일)
Li Fujian
Li Fujian 2021년 5월 25일
댓글: Li Fujian 2021년 5월 26일
When I sum a matrix by different order, the summation is different, for example:
a=rand(10)/pi;
sum(sum(a,1),2)-sum(sum(a,2),1)
the code will return a non-zeros result sometimes, which cause some problem in my calculation.
the matlab edition Im using is R2016a

채택된 답변

Chunru
Chunru 2021년 5월 25일
This is due to the numerical accuracy of the floating point operations.
Let's say we have a CPU with 4 significant digits. and we want to perform 0.1234+ 0.2345 + 345.6.
If we add in the first two small numbers, the result is 0.3579; Then we add to 345.6, we will get 346.0 (all results, intermediate or final, having 4 significant digits).
Now if we perform the addition in a reversed order. 345.6 + 0.2345 ~= 345.8; Then 345.8 + 0.1234 ~= 345.9. Thus we have different results by summing in different order.
In numerical calculation, one obtains more accurate result by performing summation of small numbers first. For example, sum of the series 1/n^2, n=1,..N, in reversed order, usually is a better choice (especially when N is very big).
Hope this help.
  댓글 수: 1
Li Fujian
Li Fujian 2021년 5월 26일
thanks for the helping. the difference is typically 10^-15 to the summation, so it doesn't make much problem. thanks again!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by