difference between mean2 and mean

31 ビュー (過去 30 日間)
Hovnatan Karapetyan
Hovnatan Karapetyan 2016 年 10 月 6 日
コメント済み: Walter Roberson 2016 年 10 月 6 日
I have a large matrix A of floats between 0 and 1. When I try mean2(A) it is very different from mean(A(:)). What could be the reason for this?
  2 件のコメント
James Tursa
James Tursa 2016 年 10 月 6 日
According to the doc mean2(A) uses mean(A(:)). How different are your results? On the order of eps of the answers?
Andrei Bobrov
Andrei Bobrov 2016 年 10 月 6 日
編集済み: Andrei Bobrov 2016 年 10 月 6 日
please example
>> a = rand(1000);
>> mean2(a)
ans =
0.5003
>> mean(a(:))
ans =
0.5003

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

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 10 月 6 日
mean() takes special care for unsigned integers and signed integers. mean2() blindly converts everything to double and totals that and divides by the number of elements. If you are using an integer data type or signed integer data type, the mean2() approach could involve unnecessary loss of precision.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 6 日
I have now submitted a documentation bug report about this, as the mean2() implementation is not as documented.

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


Marc Jakobi
Marc Jakobi 2016 年 10 月 6 日
That's strange. According to your code, there shouldn't be a different output (if A is of type double). It may be a different output if I is of type single.
The main difference is that mean2 computes the mean of all elements while mean computes the mean of each column of the matrix. However, mean2 converts the input (A) to double precision and mean doesn't. So if A is single precision the result may vary slightly if the matrix is very large.

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by