필터 지우기
필터 지우기

Simple mathematics giving unexpected outcome. Dividing data arrays/vectors

조회 수: 3 (최근 30일)
I am trying to do some calculations, but there are giving me really strange results. And I am not sure where to start looking for a solution.
I have a very simple calculation: Divide each element in the first row (or column) by the corresponding element in the second row (or column).
My input is attached;
bands 71X2 double
and I would like to get the 71 values that you get when you divide bands(:,1) by bands(2,:).
I tried this:
%original data, to ensure the right output, transpose the data:
spectralindex = bands(:,1)' ./ bands(:,2)' ;
%Or transpose the data first:
bands2 = bands';
spectralindex2 = bands2(1,:) ./ bands2(2,:) ;
%or create separate variables
band1 = bands(:,1)';
band2 = bands(:,2)';
spectralindex3 = band1 ./ band2;
Looking at the data:
bands(1,1) = 0.0709 & bands(1,2) = 0.0275. If I take a calculator and divide these, I get 0.0709 / 0.0275 = 2.578. This is the outcome I am expecting to get from matlab too. A regular division of value 1 by value 2.
However the matlab outcome shows 2.5803 as outcome.
There probably is something very logical about this, with me making a conceptual mistake on how these calculations are done. But.. I cannot figure out what. Slightly baffled. Does anyone have an idea of what is going on?

채택된 답변

Star Strider
Star Strider 2018년 2월 13일
MATLAB maintains full internal precision in its calculations. Since 2.578 and 2.5803 are not significantly different, I suspect you are seeing the effect of the reduced precision of your hand calculations and MATLAB’s extended precision calculations.
Example
d = rand(2,1)
Ratio_1 = d(1)/d(2) % Full Precision
Ratio_2 = round(d(1),4) / round(d(2),4) % Limited Precision
d =
0.712414805789522
0.016674712940232
Ratio_1 =
42.724262081335056
Ratio_2 =
42.658682634730539
  댓글 수: 3
Star Strider
Star Strider 2018년 2월 13일
Not stupid at all!
It’s just one of those things we learn as we learn about floating-point computations. Note that these have their own limitations, most significantly summarized in Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link) and How do I determine if the error in my answer is the result of round-off error or a bug? (link).
Jelle
Jelle 2018년 2월 13일
Thx. that is very kind of you. But it is the sort of mistake I should not be making anymore after doing data processing for 2 decades. Guess that moving into a new processing tools got me off guard.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by