MATLAB: Is the mean function not working

mean

clc
x=randn(1,100000);
y=randn(1,100000);
Za=x+10;
[f,x]=hist(Za,100); %Simulated PDF
bar(x,f/trapz(x,f));
hold on;
title('normalized histogram');
ym = mean(Za)
yv = var(Za)

Best Answer

I am not sure why you say that mean() is not working.
Perhaps you are figuring that the mean of the normal distribution is 0, and that when you add 10 to that, you should get a mean of exactly 10 of the result.
If so then the difficulty with that is that randn() is not the theoretical continuous normal random generator: it is the discrete normal random generator, which emits particular samples that are statistically uniform random in distribution.
The situation is exactly like flipping a single coin and expecting that the result will be a half head and a half tail because that is the long term statistical mean result. Any one coin flip must give a particular result, and although the long term statistical results will tend to 1/2 heads and 1/2 tails, the short term results will likely not be exactly balanced. It is "the Gambler's Fallacy" -- the statistics have no "memory" and probably will not exactly balance over any finite length of time.
If you want to work with theoretical continuous generators to know the theoretical statistical means instead of the discrete means over a given set of samples, then you need to use the continuous generation functions in the Statistics Toolbox.