Results of math with integer

4 ビュー (過去 30 日間)
Jonas
Jonas 2022 年 6 月 22 日
コメント済み: Jonas 2022 年 6 月 22 日
Dear community,
I recently had to work with uint8 and did some simple calculations over it. I tought using integer math tells that if we woukd result in a non integer result, the non integer part gets truncated:
a=uint8(4);
b=uint8(7);
median([a b])
ans = uint8 6
shouldn't this be 5, since the median of 4 and 7 would be the mean = 5.5 and then truncated to 5. Or is this just a matlab convention that results are rounded like doubles?
similarly
mean([a b])
ans = 5.5000
the result is a double, but shouldn't it be a uint8 (and rounded according to whatever convention)? The data type is explicitly supported in the documentation of mean.
I was just wondering.
best regards
Jonas

採用された回答

Stephen23
Stephen23 2022 年 6 月 22 日
編集済み: Stephen23 2022 年 6 月 22 日
"I tought using integer math tells that if we woukd result in a non integer result, the non integer part gets truncated:"
What you were taught is true for some other languages, but note that "MATLAB rounds the result, when necessary, according to the default rounding algorithm."
The MEDIAN() documentation states "median computes natively in the numeric class of A, such that class(M) = class(A).", which together with the rounding information above explains the output that you show (assuming that for an odd number of elements the middle two are averaged).
The MEAN() output class is given in this table:
mean(uint8([4,7]),'native')
ans = uint8 6
  1 件のコメント
Jonas
Jonas 2022 年 6 月 22 日
thanks for clearing this up.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by