MATLAB quantiz - mean square distortion

have a question regarding mean square distortion returned by MATLAB's quantiz function.

Here is my code:

dataset = rand(1,1000)*2-1;
M1 = 128;
qlevels1=-1+(2/M1):2/M1:1-(2/M1);
codebook1 =-1+(1/M1/2):2/M1:1-(1/M1/2);
[index1, quants1, dist1] = quantiz(dataset, qlevels1, codebook1);
Qnoise1 = sum((dataset-quants1).^2);


My after running my program my $Qnoise1$ and $dist1$ differ by magnitude of 1000

$Qnoise1 = 0.0374$
$dist1 = 3.7417e-05$

Am I missing something here? Should I multiply qlevels1 by length(dataset) to get correct answer?

Thanks

-
That's because you use 1000 points and sum them up. You can use mean instead of sum.
Qnoise1 = mean((dataset-quants1).^2);