Cody

# Problem 2813. Create a block diagonal matrix

Solution 2699267

Submitted on 15 Jul 2020 by Rachelle Lee
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))

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

A = [] A = [] A = [] A = []

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

A = []

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

A = [] A = 3 -2 A = 3 0 -2 0 0 3 0 -2 A = 3 0 0 -2 0 0 0 3 0 0 -2 0 0 0 3 0 0 -2 A = 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))

A = [] A = 6 A = 6 0 0 6 A = 6 0 0 0 6 0 0 0 6 A = 6 0 0 0 0 6 0 0 0 0 6 0 0 0 0 6 A = 6 0 0 0 0 0 6 0 0 0 0 0 6 0 0 0 0 0 6 0 0 0 0 0 6 A = 6 0 0 0 0 0 0 6 0 0 0 0 0 0 6 0 0 0 0 0 0 6 0 0 0 0 0 0 6 0 0 0 0 0 0 6 A = 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0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equal(block_diagonal(a,n),A_correct))

A = [] A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 A = 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))

A = [] A = 82 68 31 71 54 44 55 35 9 98 59 13 5 43 88 46 83 22 67 30 20 9 43 2 74 23 60 41 96 16 49 36 39 100 4 48 57 16 53 91 78 23 23 44 83 81 25 75 24 26 86 96 4 86 23 16 14 33 25 37 31 68 79 20 47 88 79 30 43 60 5 11 54 15 41 22 76 2 8 39 95 2 65 45 78 26 47 21 54 60 67 97 42 78 48 89 76 40 63 92 5 64 96 13

### Community Treasure Hunt

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

Start Hunting!