Mean function returning super low ( and wrong) value for a specific variable
17 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 18 Feb 2022
"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.
Answers (2)
Steven Lord
on 18 Feb 2022
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 Comments
Steven Lord
on 18 Feb 2022
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
on 18 Feb 2022
-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)?
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!