The mean, in the most general sense, is equivalent to sum(vector)/length(vector). Trying to use mean directly to compute expectations of discrete random variables makes no sense. You have two vectors, not one, in this case, vector x and vector p_x. What you want is sum(x .* p_x).
3 Likes