Cody

# Problem 2043. Six Steps to PCA - Step 1: Centre and Standardize

Solution 371673

Submitted on 17 Dec 2013 by Alfonso Nieto-Castanon
• Size: 34
• This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% m = 100; n = 10; x = rand(m,n); k = false(1,n); k(randi(n,1,3)) = true; x(:,k) = rand; tol = 1.0e-12; out = zscore(x); assert(isstruct(out),'Struct test failed.') assert(isfield(out,'Z'),'Z field test failed.') assert(isfield(out,'Mu'),'Mu field test failed.') assert(isfield(out,'Sigma'),'Sigma field test failed.') assert(isequal(size(x),size(out.Z)),'Z size test failed.') assert(isequal(size(x(1,:)),size(out.Mu)),'Mu size test failed.') assert(isequal(size(x(1,:)),size(out.Sigma)),'Sigma size test failed.') assert(all(abs(mean(out.Z))<tol),'Mean test failed.') assert(all(abs(std(out.Z(:,~k))-1)<tol),'STD test failed.') assert(all(abs(std(out.Z(:,k)))<tol),'STD test for invariant data failed.') assert(isequal(out.Mu,mean(x,1)),'Mean equality test failed.') assert(isequal(out.Sigma,std(x,0,1)),'STD equality test failed.')

2   Pass
%% A second pass known to produce unsafe results with Matlab's zscore m = 100; n = 10; rng(2000,'twister') x = rand(m,n); k = false(1,n); k(randi(n,1,3)) = true; x(:,k) = rand; tol = 1.0e-12; out = zscore(x); assert(isstruct(out),'Struct test failed.') assert(isfield(out,'Z'),'Z field test failed.') assert(isfield(out,'Mu'),'Mu field test failed.') assert(isfield(out,'Sigma'),'Sigma field test failed.') assert(isequal(size(x),size(out.Z)),'Z size test failed.') assert(isequal(size(x(1,:)),size(out.Mu)),'Mu size test failed.') assert(isequal(size(x(1,:)),size(out.Sigma)),'Sigma size test failed.') assert(all(abs(mean(out.Z))<tol),'Mean test failed.') assert(all(abs(std(out.Z(:,~k))-1)<tol),'STD test failed.') assert(all(abs(std(out.Z(:,k)))<tol),'STD test for invariant data failed.') assert(isequal(out.Mu,mean(x,1)),'Mean equality test failed.') assert(isequal(out.Sigma,std(x,0,1)),'STD equality test failed.')

### Community Treasure Hunt

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

Start Hunting!