Cody

# Problem 2813. Create a block diagonal matrix

Solution 1911283

Submitted on 24 Aug 2019
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 = [1 2 3]; n = 3; A_correct = [1 2 3 0 0 0 0 0 0; 0 0 0 1 2 3 0 0 0; 0 0 0 0 0 0 1 2 3]; assert(isequal(block_diagonal(a,n),A_correct))

c = 1 2 3 0 0 0 0 0 0 t = 0 0 0 1 2 3 0 0 0 t = 0 0 0 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 A = 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3

2   Fail
a = []; n = 3; A_correct = []; assert(isequal(block_diagonal(a,n),A_correct))

c = 1×0 empty double row vector t = 1×0 empty double row vector t = 2×0 empty double matrix A = 3×0 empty double matrix

Assertion failed.

3   Fail
a = [1 2 -3]'; n = 0; A_correct = []; assert(isequal(block_diagonal(a,n),A_correct))

c = 1 2 -3 A = 1 2 -3

Assertion failed.

4   Fail
a = [3 -2]'; n = 4; A_correct = [3 -2 0 0 0 0 0 0; 0 0 3 -2 0 0 0 0; 0 0 0 0 3 -2 0 0; 0 0 0 0 0 0 3 -2]'; assert(isequal(block_diagonal(a,n),A_correct))

c = 3 -2 0 0 0 0 0 0 t = 0 0 3 -2 0 0 0 0 t = 0 0 3 -2 0 0 0 0 0 0 0 0 3 -2 0 0 t = 0 0 3 -2 0 0 0 0 0 0 0 0 3 -2 0 0 0 0 0 0 0 0 3 -2 A = 3 -2 0 0 0 0 0 0 0 0 3 -2 0 0 0 0 0 0 0 0 3 -2 0 0 0 0 0 0 0 0 3 -2

Assertion failed.

5   Pass
a = 6; n = 23; A_correct = a*eye(n); assert(isequal(block_diagonal(a,n),A_correct))

c = 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 t = 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 t = 0 6 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0...

6   Fail
a = magic(5); n = 2; A_correct = [a zeros(5); zeros(5) a]; assert(isequal(block_diagonal(a,n),A_correct))

c = Columns 1 through 30 17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9 0 0 0 0 0 Columns 31 through 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 t = Columns 1 through 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 23 4 10 11 Columns 31 through 50 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9 A = Columns 1 through 30 17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 23 4 10 11 Columns 31 through 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9

Assertion failed.

7   Fail
a = randi(100,13,8); n = 1; A_correct = a; assert(isequal(block_diagonal(a,n),A_correct))

c = Columns 1 through 30 56 2 37 72 76 55 11 45 53 35 40 82 100 32 85 95 72 50 63 31 48 31 62 83 42 66 68 60 88 11 Columns 31 through 60 4 55 79 11 41 94 57 84 57 30 64 65 61 16 57 27 81 55 36 94 100 1 41 78 12 43 10 22 52 96 Columns 61 through 90 31 92 78 29 80 84 12 38 56 26 26 28 45 75 17 49 100 12 78 47 67 7 20 15 13 46 77 46 57 92 Columns 91 through 104 23 23 22 1 52 62 8 59 76 33 53 80 53 64 A = Columns 1 through 30 56 2 37 72 76 55 11 45 53 35 40 82 100 32 85 95 72 50 63 31 48 31 62 83 42 66 68 60 88 11 Columns 31 through 60 4 55 79 11 41 94 57 84 57 30 64 65 61 16 57 27 81 55 36 94 100 1 41 78 12 43 10 22 52 96 Columns 61 through 90 31 92 78 29 80 84 12 38 56 26 26 28 45 75 17 49 100 12 78 47 67 7 20 15 13 46 77 46 57 92 Columns 91 through 104 23 23 22 1 52 62 8 59 76 33 53 80 53 64

Assertion failed.