Cody

# Problem 2813. Create a block diagonal matrix

Solution 1939486

Submitted on 17 Sep 2019 by Augusto Mazzei
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 = 93 89 29 4 12 62 91 89 24 57 80 68 11 34 32 30 70 18 8 50 88 52 54 60 84 6 36 37 1 36 83 19 65 54 13 50 82 95 35 88 68 28 78 27 78 80 80 74 11 80 22 63 8 61 48 88 15 21 43 71 95 93 42 44 21 10 66 93 78 1 37 98 98 90 26 8 44 70 49 21 16 28 55 60 52 29 71 69 9 2 44 62 81 38 62 61 53 14 87 3 91 58 3 7

### Community Treasure Hunt

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

Start Hunting!