Mean function returning super low ( and wrong) value for a specific variable

17 views (last 30 days)
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
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]
a = 9×1
0.0139 0.0205 0.0023 0.0236 -0.0104 -0.0164 -0.0164 -0.0385 0.0215
mean(a)
ans = 1.1025e-16
"I really don't know what is wrong with it."
So far you haven't shown that anything is wrong with MEAN.

Sign in to comment.

Answers (2)

Steven Lord
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)
ans = 1.0000e-04
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
theMean = -2.2222e-17
  5 Comments
Salvino Macêdo
Salvino Macêdo on 18 Feb 2022
Edited: Salvino Macêdo on 18 Feb 2022
Hi Jan, thanks for your reply.
I've tried using format long g, but I got the same results.
So, as an experiment, I copy and pasted (using ctrl +c ctrl v) the values from the Matlab variables window to excel
This is what I got:
The values from A1:A8 are straight up copy and pasted from matlab, the values from D1:D8 I typed in manually.
What is going on? really ahhaha I'm starting to get a little bit crazy.
EDIT:
So to add a little bit of info. I'm not relying solely on the cmd window, these values (1.9e-17) are showing up on the variables window as well
Steven Lord
Steven Lord on 18 Feb 2022
As a simpler example, what's 1 divided by 3?
x = 1/3
x = 0.3333
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
y = 0.9999
z = 3*x
z = 1
Is x exactly 0.3333?
difference = x - 0.3333 % No.
difference = 3.3333e-05

Sign in to comment.


Voss
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]
a = 9×1
0.0139 0.0205 0.0023 0.0236 -0.0104 -0.0164 -0.0164 -0.0385 0.0215
sum(a)
ans = 1.0000e-04
mean(a)
ans = 1.1111e-05
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
Salvino Macêdo on 18 Feb 2022
Thank you for your response.
I am working with low values in my research, so 1e-4 or 1e-5 is a significant value to me!
What i noticed is that Mesh.U and U_BC (the variables which compose Mesh.U2) have the same mean value. However, that still doesnt make any sense to me.

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!