MATLAB: Does the ZSCORE function in the Statistics Toolbox provide me with unexpected results

Statistics and Machine Learning Toolboxstdzscore

If I input a small data set into the ZSCORE function in MATLAB, I see one set of results:
A = [1; 2; 3];
out = zscore(A)
out =
-1
0
1
If I double the data set, I expect similar results. I instead see a change in scale:
A = [A; A];
out = zscore(A)
out =
-1.1180
0
1.1180
-1.1180
0
1.1180%

Best Answer

This enhancement has been incorporated in Statistics Toolbox 6.2 (R2008a). For previous product releases, read below for any possible workarounds:
This issue arises with how MATLAB defines the standard deviation of a data set. By default, MATLAB normalizes the standard deviation by a factor of (N-1), where N is the number of data points in the set. In order to see the behavior you like from the z-score, you must instead compute the standard deviation in an alternate way that normalizes by a factor of N. This can be done with the following code:
function [z,mu,sigma] = zscore2(x)
if isequal(x,[]), z = []; return; end
mu = mean(x);
sigma = std(x,1);
sigma0 = sigma;
sigma0(sigma0==0) = 1;
z = bsxfun(@minus,x, mu);
z = bsxfun(@rdivide, z,sigma0);