Cody

# Problem 2049. Six Steps to PCA - Step 2: Covariance

Solution 1657240

Submitted on 23 Oct 2018 by Thomas Steffen
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; rng(2000,'twister') x = rand(m,n); k = false(1,n); k(randi(n,1,3)) = true; x(:,k) = rand; tol = 1.0e-12; s = covs(x); assert(isstruct(s),'Struct test failed.') assert(isfield(s,'Z'),'Z field test failed.') assert(isfield(s,'Mu'),'Mu field test failed.') assert(isfield(s,'Sigma'),'Sigma field test failed.') assert(isequal(size(x),size(s.Z)),'Z size test failed.') assert(isequal(size(x(1,:)),size(s.Mu)),'Mu size test failed.') assert(isequal(size(x(1,:)),size(s.Sigma)),'Sigma size test failed.') assert(all(abs(mean(s.Z))<tol),'Mean test failed.') assert(all(abs(std(s.Z(:,~k))-1)<tol),'STD test failed.') assert(all(abs(std(s.Z(:,k)))<tol),'STD test for invariant data failed.') assert(isequal(s.Mu,mean(x,1)),'Mean equality test failed.') assert(isequal(s.Sigma,std(x,0,1)),'STD equality test failed.') assert(isfield(s,'Cov'),'Cov field test failed.') assert(all(all(abs(s.Cov-cov(s.Z))<1e-12)),'Cov equality test failed.')

2   Pass
str = fileread('covs.m'); assert(isempty(regexp(str,'=[ ]*cov[ ]*(')),'Don''t call Matlab''s own cov function.') assert(isempty(regexp(str,'@[\s\.'']*c[\s\.'']*o[\s\.'']*v')),'This could go on a while :-)') assert(numel(regexp(str,'zscore'))>=2,'Keep to the structure of the original template.')