Cody

# Problem 1972. Convert matrix to 3D array of triangular matrices

Solution 1578526

Submitted on 9 Jul 2018 by Yuichi Tadokoro
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
x = 1:100; y_correct = shiftdim(x,-1); assert(isequal(mat2triu3(x),y_correct))

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 y(:,:,37) = 37 y(:,:,38) = 38 y(:,:,39) = 39 y(:,:,40) = 40 y(:,:,41) = 41 y(:,:,42) = 42 y(:,:,43) = 43 y(:,:,44) = 44 y(:,:,45) = 45 y(:,:,46) = 46 y(:,:,47) = 47 y(:,:,48) = 48 y(:,:,49) = 49 y(:,:,50) = 50 y(:,:,51) = 51 y(:,:,52) = 52 y(:,:,53) = 53 y(:,:,54) = 54 y(:,:,55) = 55 y(:,:,56) = 56 y(:,:,57) = 57 y(:,:,58) = 58 y(:,:,59) = 59 y(:,:,60) = 60 y(:,:,61) = 61 y(:,:,62) = 62 y(:,:,63) = 63 y(:,:,64) = 64 y(:,:,65) = 65 y(:,:,66) = 66 y(:,:,67) = 67 y(:,:,68) = 68 y(:,:,69) = 69 y(:,:,70) = 70 y(:,:,71) = 71 y(:,:,72) = 72 y(:,:,73) = 73 y(:,:,74) = 74 y(:,:,75) = 75 y(:,:,76) = 76 y(:,:,77) = 77 y(:,:,78) = 78 y(:,:,79) = 79 y(:,:,80) = 80 y(:,:,81) = 81 y(:,:,82) = 82 y(:,:,83) = 83 y(:,:,84) = 84 y(:,:,85) = 85 y(:,:,86) = 86 y(:,:,87) = 87 y(:,:,88) = 88 y(:,:,89) = 89 y(:,:,90) = 90 y(:,:,91) = 91 y(:,:,92) = 92 y(:,:,93) = 93 y(:,:,94) = 94 y(:,:,95) = 95 y(:,:,96) = 96 y(:,:,97) = 97 y(:,:,98) = 98 y(:,:,99) = 99 y(:,:,100) = 100

2   Pass
x = reshape(1:15,3,[]); y_correct(:,:,1) = [1 2;0 3]; y_correct(:,:,2) = [4 5;0 6]; y_correct(:,:,3) = [7 8;0 9]; y_correct(:,:,4) = [10 11;0 12]; y_correct(:,:,5) = [13 14;0 15]; assert(isequal(mat2triu3(x),y_correct))

y(:,:,1) = 1 2 0 3 y(:,:,2) = 4 5 0 6 y(:,:,3) = 7 8 0 9 y(:,:,4) = 10 11 0 12 y(:,:,5) = 13 14 0 15

3   Pass
x = reshape(1:18,3,[])'; y_correct(:,:,1) = [1 4 10; 0 7 13; 0 0 16]; y_correct(:,:,2) = [2 5 11; 0 8 14; 0 0 17]; y_correct(:,:,3) = [3 6 12; 0 9 15; 0 0 18]; assert(isequal(mat2triu3(x),y_correct))

y(:,:,1) = 1 4 10 0 7 13 0 0 16 y(:,:,2) = 2 5 11 0 8 14 0 0 17 y(:,:,3) = 3 6 12 0 9 15 0 0 18

4   Pass
x = randi(50,sum(1:100),22); y = mat2triu3(x); mask = (y~=0); xb = reshape(y(mask),[],size(y,3)); assert(isequal(size(y),[100 100 22])) assert(isequal(x,xb))

y(:,:,1) = Columns 1 through 29 18 16 26 39 28 44 28 18 4 23 28 26 9 44 42 9 34 14 29 5 49 14 39 33 24 23 10 27 43 0 13 27 24 22 25 34 12 24 24 43 39 4 2 7 13 14 49 2 24 24 28 1 36 10 50 19 26 21 0 0 49 24 36 41 43 26 14 32 49 46 46 36 44 25 42 35 31 50 40 39 49 13 42 25 2 15 1 0 0 0 39 41 39 9 44 14 5 21 9 35 49 49 29 42 15 16 48 5 45 47 30 4 19 4 14 2 0 0 0 0 41 45 4 19 4 8 24 27 1 36 12 36 50 41 29 27 38 3 47 21 23 41 6 28 29 0 0 0 0 0 41 34 16 19 27 9 32 12 19 27 46 29 19 11 2 44 3 25 6 9 47 2 47 4 0 0 0 0 0 0 9 9 34 44 35 47 19 30 15 4 34 14 21 31 12 5 1 10 42 13 41 35 12 0 0 0 0 0 0 0 18 24 20 14 7 24 12 9 48 42 28 45 1 8 14 35 4 25 12 44 6 36 0 0 0 0 0 0 0 0 13 46 16 7 50 24 14 36 8 23 44 41 18 47 42 21 33 31 3 11 32 0 0 0 0 0 0 0 0 0 44 36 20 43 23 2 21 32 30 48 17 4 42 5 49 7 32 10 9 7 0 0 0 0 0 0 0 0 0 0 50 37 47 18 19 30 34 44 41 24 38 25 20 27 45 16 15 17 27 0 0 0 0 0 0 0 0 0 0 0 42 43 4 35 5 39 10 14 41 22 37 17 22 31 36 37 23 39 0 0 0 0 0 0 0 0 0 0 0 0 26 38 35 20 18 6 22 33 9 48 17 18 18 29 3 48 47 0 0 0 0 0 0 0 0 0 0 0 0 0 26 48 44 30 5 30 19 13 46 16 11 5 2 45 29 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 31 30 38 40 15 42 5 31 11 39 49 33 3 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 19 11 19 1 36 6 24 22 42 26 42 10 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 21 9 32 49 27 15 36 7 42 21 22 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 36 13 9 31 47 28 38 48 4 50 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 29 11 34 25 29 38 32 36 33 35 0 0 0 0 0 0 0 0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