Cody

# Problem 2813. Create a block diagonal matrix

Solution 1709931

Submitted on 18 Jan 2019 by HH
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))

ans = 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   Pass
a = []; n = 3; A_correct = []; assert(isequal(block_diagonal(a,n),A_correct))

ans = []

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

ans = []

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

ans = 3 0 0 0 -2 0 0 0 0 3 0 0 0 -2 0 0 0 0 3 0 0 0 -2 0 0 0 0 3 0 0 0 -2

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

ans = 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 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 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 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 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 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 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 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 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 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 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

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

ans = 17 24 1 8 15 0 0 0 0 0 23 5 7 14 16 0 0 0 0 0 4 6 13 20 22 0 0 0 0 0 10 12 19 21 3 0 0 0 0 0 11 18 25 2 9 0 0 0 0 0 0 0 0 0 0 17 24 1 8 15 0 0 0 0 0 23 5 7 14 16 0 0 0 0 0 4 6 13 20 22 0 0 0 0 0 10 12 19 21 3 0 0 0 0 0 11 18 25 2 9

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

ans = 66 6 81 28 71 58 74 43 39 83 19 100 50 58 12 74 88 15 26 83 40 42 67 49 97 49 3 74 76 64 42 4 64 45 57 34 100 18 65 55 33 4 49 41 12 60 46 66 5 6 99 70 20 71 74 91 77 15 91 50 26 89 55 21 96 14 50 74 29 75 68 50 15 41 58 74 60 93 45 64 90 95 68 56 71 61 33 85 50 28 57 93 29 17 54 36 43 97 99 61 54 11 58 42