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

Solution 372370

Submitted on 19 Dec 2013 by J.R.! Menzinger
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.')

ans = Mu: [0.6111 0.6111 0.5447 0.5048 0.4942 0.5449 0.5643 0.4452 0.5198 0.5210] Sigma: [1.2274e-15 1.2274e-15 0.2893 0.2963 0.2767 0.2913 0.2787 0.2975 0.2883 0.2876]

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.')

Suggested Problems

More from this Author1

Community Treasure Hunt

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

Start Hunting!