Simple mathematics giving unexpected outcome. Dividing data arrays/vectors

3 ビュー (過去 30 日間)
Jelle
Jelle 2018 年 2 月 12 日
コメント済み: Jelle 2018 年 2 月 13 日
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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by