フィルターのクリア

floor((I3(​i,j-1)+I3(​i-1,j))/2)

1 回表示 (過去 30 日間)
RENJI
RENJI 2023 年 8 月 14 日
コメント済み: RENJI 2023 年 8 月 15 日
the expression evaluates to 192.5, but the function returns 193 instead of 192. Please help me out with this problem.
  3 件のコメント
RENJI
RENJI 2023 年 8 月 15 日
In my program it is 193, but when I type in command prompt it is 192.
RENJI
RENJI 2023 年 8 月 15 日
I want it in my computation, not just for printing the value...in the whole computation there is a mismatch because of this rounding error.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 15 日
移動済み: Bruno Luong 2023 年 8 月 15 日
@RENJI "the expression evaluates to 192.5"
The above is a FALSE statement since (I3(i,j-1)+I3(i-1,j))/2 cannot have fractional digit provided that I3 is of class uint32.
Solution: convert your array to double to do floating point arithmetics
I3d = double(I3);
floor((I3d(i,j-1)+I3d(i-1,j))/2)
  1 件のコメント
RENJI
RENJI 2023 年 8 月 15 日
Thank you very much...its working fine now...thanks for your constant support.

サインインしてコメントする。

その他の回答 (3 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 8 月 14 日
To get the displayed data w.r.t your format specs:
ANS = 192.5;
% One decimal point
fprintf('Solution = %.1f \n', ANS)
Solution = 192.5
% OR two decimal points
fprintf('Solution = %.2f \n', ANS)
Solution = 192.50
% OR round up towards to -inf as Fangjun suggested
fprintf('Solution = %d \n', floor(ANS))
Solution = 192

Bruno Luong
Bruno Luong 2023 年 8 月 15 日
編集済み: Bruno Luong 2023 年 8 月 15 日
Set a breat point in your program, when it breaks type this and report the result
I3(i,j-1)
I3(i-1,j)
(I3(i,j-1)+I3(i-1,j))/2
floor(ans)
exist('floor')
which floor((I3(i,j-1)+I3(i-1,j))/2)

Walter Roberson
Walter Roberson 2023 年 8 月 15 日
You values are integer data class, such as uint8() . When you do calculations on integer data type, the result is calculated as if the values were converted to double, then the value was calculated, and then the result was cast to the integer data type. But when you cast a floating point number to an integer data type, the value is rounded. So the internal 192.5 that is calculated from the / operation is uint8() and converting to integer class rounds so it is rounded to uint8(193) . Then you are applying floor() to the uint8 193 which of course is still 193.
  4 件のコメント
Walter Roberson
Walter Roberson 2023 年 8 月 15 日
floor((double(I3(i,j-1))+double(I3(i-1,j)))/2)
but if you were doing a bunch of these, convert the entire I3 array to double and index the double array.
D3 = double(I3);
floor((D3(i,j-1)+D3(i-1,j))/2)
RENJI
RENJI 2023 年 8 月 15 日
Thank you very much...its working fine now...

サインインしてコメントする。

カテゴリ

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