Cody

# Problem 44350. Breaking Out of the Matrix

Solution 2106066

Submitted on 26 Jan 2020 by avner yakov
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
M=[1 4 7 10; 2 5 8 11; 3 6 9 12]; R=2;C=3; X(:,:,1) =[1 4 7 ; 2 5 8]; X(:,:,2) =[2 5 8 ; 3 6 9]; X(:,:,3) =[4 7 10 ; 5 8 11]; X(:,:,4) =[5 8 11 ; 6 9 12]; assert(isequal(BreakTheMatrix(M,R,C),X))

r = 3 c = 4 num_r = 2 num_c = 2 ans = 2 3 4 y(:,:,1) = 1 4 7 2 5 8 y(:,:,2) = 2 5 8 3 6 9 y(:,:,3) = 4 7 10 5 8 11 y(:,:,4) = 5 8 11 6 9 12

2   Pass
x=1:ceil(35+25*rand());r=1;c=1; M=BreakTheMatrix(x,r,c); assert(all(arrayfun(@(y) (M(:,:,y)==y),1:numel(x))))

r = 1 c = 36 num_r = 1 num_c = 36 ans = 1 1 36 y(:,:,1) = 1 y(:,:,2) = 2 y(:,:,3) = 3 y(:,:,4) = 4 y(:,:,5) = 5 y(:,:,6) = 6 y(:,:,7) = 7 y(:,:,8) = 8 y(:,:,9) = 9 y(:,:,10) = 10 y(:,:,11) = 11 y(:,:,12) = 12 y(:,:,13) = 13 y(:,:,14) = 14 y(:,:,15) = 15 y(:,:,16) = 16 y(:,:,17) = 17 y(:,:,18) = 18 y(:,:,19) = 19 y(:,:,20) = 20 y(:,:,21) = 21 y(:,:,22) = 22 y(:,:,23) = 23 y(:,:,24) = 24 y(:,:,25) = 25 y(:,:,26) = 26 y(:,:,27) = 27 y(:,:,28) = 28 y(:,:,29) = 29 y(:,:,30) = 30 y(:,:,31) = 31 y(:,:,32) = 32 y(:,:,33) = 33 y(:,:,34) = 34 y(:,:,35) = 35 y(:,:,36) = 36

3   Pass
x=eye(7);r=2;c=2; M=BreakTheMatrix(x,r,c); ids=[1 8 15 22 29 36]; urs=ids(1:5)+1; lls=urs+5; z=setxor(1:size(M,3),[ids urs lls]); a1=arrayfun(@(a) isequal(M(:,:,a),eye(2)),ids); a2=arrayfun(@(a) isequal(M(:,:,a),[0 1 ; 0 0]),urs); a3=arrayfun(@(a) isequal(M(:,:,a),[0 0 ; 1 0]),lls); a4=arrayfun(@(a) isequal(M(:,:,a),zeros(2)),z); assert(all([a1 a2 a3 a4]))

r = 7 c = 7 num_r = 6 num_c = 6 ans = 2 2 36 y(:,:,1) = 1 0 0 1 y(:,:,2) = 0 1 0 0 y(:,:,3) = 0 0 0 0 y(:,:,4) = 0 0 0 0 y(:,:,5) = 0 0 0 0 y(:,:,6) = 0 0 0 0 y(:,:,7) = 0 0 1 0 y(:,:,8) = 1 0 0 1 y(:,:,9) = 0 1 0 0 y(:,:,10) = 0 0 0 0 y(:,:,11) = 0 0 0 0 y(:,:,12) = 0 0 0 0 y(:,:,13) = 0 0 0 0 y(:,:,14) = 0 0 1 0 y(:,:,15) = 1 0 0 1 y(:,:,16) = 0 1 0 0 y(:,:,17) = 0 0 0 0 y(:,:,18) = 0 0 0 0 y(:,:,19) = 0 0 0 0 y(:,:,20) = 0 0 0 0 y(:,:,21) = 0 0 1 0 y(:,:,22) = 1 0 0 1 y(:,:,23) = 0 1 0 0 y(:,:,24) = 0 0 0 0 y(:,:,25) = 0 0 0 0 y(:,:,26) = 0 0 0 0 y(:,:,27) = 0 0 0 0 y(:,:,28) = 0 0 1 0 y(:,:,29) = 1 0 0 1 y(:,:,30) = 0 1 0 0 y(:,:,31) = 0 0 0 0 y(:,:,32) = 0 0 0 0 y(:,:,33) = 0 0 0 0 y(:,:,34) = 0 0 0 0 y(:,:,35) = 0 0 1 0 y(:,:,36) = 1 0 0 1

4   Pass
u=ceil(10*rand())+4; x=magic(u);r=u;c=u; M=BreakTheMatrix(x,r,c); assert(isequal(M,x))

r = 12 c = 12 num_r = 1 num_c = 1 ans = 12 12 y = 144 2 3 141 140 6 7 137 136 10 11 133 13 131 130 16 17 127 126 20 21 123 122 24 25 119 118 28 29 115 114 32 33 111 110 36 108 38 39 105 104 42 43 101 100 46 47 97 96 50 51 93 92 54 55 89 88 58 59 85 61 83 82 64 65 79 78 68 69 75 74 72 73 71 70 76 77 67 66 80 81 63 62 84 60 86 87 57 56 90 91 53 52 94 95 49 48 98 99 45 44 102 103 41 40 106 107 37 109 35 34 112 113 31 30 116 117 27 26 120 121 23 22 124 125 19 18 128 129 15 14 132 12 134 135 9 8 138 139 5 4 142 143 1

5   Pass
temp=ceil(8*rand)+3; x=ones(temp);r=2;c=2; M=BreakTheMatrix(x,r,c); assert(size(M,3)==(temp-1)^2); assert(all(arrayfun(@(a) isequal(M(:,:,a),ones(2)),1:size(M,3))))

r = 6 c = 6 num_r = 5 num_c = 5 ans = 2 2 25 y(:,:,1) = 1 1 1 1 y(:,:,2) = 1 1 1 1 y(:,:,3) = 1 1 1 1 y(:,:,4) = 1 1 1 1 y(:,:,5) = 1 1 1 1 y(:,:,6) = 1 1 1 1 y(:,:,7) = 1 1 1 1 y(:,:,8) = 1 1 1 1 y(:,:,9) = 1 1 1 1 y(:,:,10) = 1 1 1 1 y(:,:,11) = 1 1 1 1 y(:,:,12) = 1 1 1 1 y(:,:,13) = 1 1 1 1 y(:,:,14) = 1 1 1 1 y(:,:,15) = 1 1 1 1 y(:,:,16) = 1 1 1 1 y(:,:,17) = 1 1 1 1 y(:,:,18) = 1 1 1 1 y(:,:,19) = 1 1 1 1 y(:,:,20) = 1 1 1 1 y(:,:,21) = 1 1 1 1 y(:,:,22) = 1 1 1 1 y(:,:,23) = 1 1 1 1 y(:,:,24) = 1 1 1 1 y(:,:,25) = 1 1 1 1

6   Pass
x=eye(7);r=7;c=7; assert(isequal(x,BreakTheMatrix(x,r,c)))

r = 7 c = 7 num_r = 1 num_c = 1 ans = 7 7 y = 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1