Cody

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

Solution 2925328

Submitted on 8 Sep 2020 by Jens Kjærgaard Boldsen
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))

sz = 1 100 n = 1 ix = 0 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))

sz = 3 5 n = 2 ix = 0 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))

sz = 6 3 n = 3 ix = 0 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))

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

Suggested Problems

More from this Author44

Community Treasure Hunt

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

Start Hunting!