Mean function returning super low ( and wrong) value for a specific variable
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
"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)
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
Salvino Macêdo
on 18 Feb 2022
Jan
on 18 Feb 2022
@Salvino Macêdo: Do not rely on displayed values in the command window. Remember, that the display is rounded. At first expand the number of shown digits:
format long g
Now check the contents of the variable a again.
It would be pure magic, if the mean() command replies wrong results, aver it was used for 25 years in millions of Matlab sessions successfully. Whenever such magic occurs, be sure, that you have overseen a simple detail.
Steven Lord
on 18 Feb 2022
What boogles my mind is that, if I just type by hand this exact array I get the real mean value of 1.111e-5 instead of -1.9e-17.
That's because what you typed by hand is NOT the "exact array" a. Do me a favor and run these two lines of code, using the original a as you computed NOT as you typed it.
a2 = [0.0139; 0.0205; 0.0023; 0.0236; -0.0104; -0.0164; -0.0164; -0.0385; 0.0215];
difference = a - a2
Not all of the values of difference will be 0. Those differences between the elements of the computed array a and the typed in array a2 don't affect how the array a is displayed but does affect the results of computations using a.
Salvino Macêdo
on 18 Feb 2022
Edited: Salvino Macêdo
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.
-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)?
1 Comment
Salvino Macêdo
on 18 Feb 2022
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!