Cody

Problem 3072. Singular Value Decomposition

Solution 3023887

Submitted on 1 Oct 2020 by Ahsanul Islam
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
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]))

2   Pass
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]))

3   Pass
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)

4   Pass
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)

5   Pass
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

6   Pass
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

7   Pass
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

Suggested Problems

More from this Author139

Community Treasure Hunt

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

Start Hunting!