Mean function returning super low ( and wrong) value for a specific variable
조회 수: 17 (최근 30일)
이전 댓글 표시
Hello!
I've been trying to obtain a simple mean value of an array inside a cell variable. However, I keep getting these bizarre and super low results.
If I create another array by typing the values by hand, I get the correct mean value, however, If I "copy" the values from this variable to another I get this other result.

The variable in question is Mesh.U2. and the content is this simple array of values. You can notice just by looking at the values that the real mean is nowhere close to -1.9e-17.
I really don't know what is wrong with it.
The variable Mesh.U2 is obtained by:
for i = 1:length(U_BC)
U_BC{1,i}(:,3) = zeros(length(Mesh.nodes),1);
Mesh.U2{i}= Mesh.U{i}(:) - U_BC{i}(:);
end
If I try to get the mean values of Mesh.U and U_BC separately, I get an accurate result.
Thanks in advance
댓글 수: 1
Stephen23
2022년 2월 18일
"You can notice just by looking at the values that the real mean is nowhere close to -1.9e-17."
Nope, I don't notice that at all.
It is easy to define data that look exactly like what you show, but average to close to zero:
a = [0.013850000000001;0.0205;0.0023;0.0236;-0.0104;-0.0164;-0.0164;-0.0385;0.02145]
mean(a)
"I really don't know what is wrong with it."
So far you haven't shown that anything is wrong with MEAN.
답변 (2개)
Steven Lord
2022년 2월 18일
You can notice just by looking at the values that the real mean is nowhere close to -1.9e-17.
Why can't the mean be close to 0? Note that the data below is not exactly the data stored in your matrix a, it matches that data to the four decimal places to which your data was displayed. Use the format function if you want to display more decimal places.
a = [0.0139; 0.0205; 0.0023; 0.0236; -0.0104; -0.0164; -0.0164; -0.0385; 0.0215];
sum(a)
If we were to take into account the full precision of your data, that sum could very well be a number on the order of -2e-16. If it were:
theMean = -2e-16/9
댓글 수: 5
Steven Lord
2022년 2월 18일
As a simpler example, what's 1 divided by 3?
x = 1/3
Is what is displayed above as x exactly one third? Or is it one third rounded off to a finite number of decimal places?
y = 3*0.3333 % Using the displayed value DOES NOT give us 1
z = 3*x
Is x exactly 0.3333?
difference = x - 0.3333 % No.
Voss
2022년 2월 18일
-1.9275e-17 is pretty close to 0, so that seems plausible to me, given a.
Using the values as displayed, with 4 decimal points of precision:
a = [ ...
0.0139; ...
0.0205; ...
0.0023; ...
0.0236; ...
-0.0104; ...
-0.0164; ...
-0.0164; ...
-0.0385; ...
0.0215]
sum(a)
mean(a)
Also close to 0 (closer than any element of a is).
What is the accurate value you get using the other variables (which I don't have)?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
