Cody

# Problem 3072. Singular Value Decomposition

Solution 3023884

Submitted on 1 Oct 2020 at 21:26
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   Fail
A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1]))

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test1 (line 2) [U,S,V] = sing_val_decomp(A);

2   Fail
A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0]))

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test2 (line 2) [U,S,V] = sing_val_decomp(A);

3   Fail
A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 -1; 1 1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[1;-0.5;0],0.1826*[-1;-2;5]]))))<5e-3)

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test3 (line 2) [U,S,V] = sing_val_decomp(A);

4   Fail
A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5)

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test4 (line 2) [U,S,V] = sing_val_decomp(A);

5   Fail
ind = randi(4); switch ind case 1 A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1])) case 2 A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0])) case 3 A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4) case 4 A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5) end

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test5 (line 5) [U,S,V] = sing_val_decomp(A);

6   Fail
ind = randi(4); switch ind case 1 A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1])) case 2 A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0])) case 3 A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4) case 4 A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5) end

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test6 (line 23) [U,S,V] = sing_val_decomp(A);

7   Fail
ind = randi(4); switch ind case 1 A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1])) case 2 A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0])) case 3 A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4) case 4 A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5) end

Undefined function 'pca' for input arguments of type 'double'. Error in sing_val_decomp (line 2) [U S V]=pca(A); Error in Test7 (line 17) [U,S,V] = sing_val_decomp(A);

### Community Treasure Hunt

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

Start Hunting!