Cody

# Problem 375. N-Dimensional Array Slice

Solution 1410514

Submitted on 8 Jan 2018 by Thomas Steffen
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 = randn(5,5); assert(isequal(arraySlice(A,4,2),A(:,4)))

ns = 2 1 A1 = 0.5895 -1.5075 0.5708 -0.0049 -0.0153 0.6344 -0.7260 -1.1512 1.8826 0.0656 0.9053 -0.1687 1.0841 -0.7985 1.6686 -1.1769 -1.1122 0.7358 -0.3676 -0.0604 1.4965 1.3035 -0.0108 1.0777 -0.6283 A2 = -1.1769 -1.1122 0.7358 -0.3676 -0.0604 S = -1.1769 -1.1122 0.7358 -0.3676 -0.0604

2   Pass
A = randn(5,5); assert(isequal(arraySlice(A,4,1),A(4,:)))

ns = 1 2 A1 = -0.8156 1.3178 -0.7438 0.1271 -0.2645 0.2010 0.9722 -0.1607 1.6178 0.8147 0.4288 -0.2193 -2.5231 -0.4125 -1.2499 0.6885 -0.2425 1.6612 -1.7201 -0.5695 0.6045 -1.0004 1.0689 1.4603 1.0010 A2 = 0.6885 -0.2425 1.6612 -1.7201 -0.5695 S = 0.6885 -0.2425 1.6612 -1.7201 -0.5695

3   Pass
A = randn(5,5); assert(isequal(arraySlice(A,1,10),A))

4   Pass
A = randn(5,5,5,3); assert(isequal(arraySlice(A,3,4),A(:,:,:,3)))

ns = 4 1 2 3 A1(:,:,1,1) = -1.3331 -0.1255 -0.5654 0.7049 -0.8726 0.4647 -1.1617 -1.4990 0.7061 -0.9653 -0.0616 -1.0761 0.5527 -0.0084 2.0803 A1(:,:,2,1) = 2.3978 0.9208 -0.0139 -1.4202 0.9134 1.6862 -0.3976 1.7968 -0.4916 -0.2092 -0.7017 -1.1692 0.8001 -1.6007 2.1820 A1(:,:,3,1) = 0.6924 -0.0241 -0.0040 1.0235 -0.8223 -0.5001 0.4374 -0.7330 0.5935 0.1395 0.7921 0.7887 0.2248 0.2636 -0.3587 A1(:,:,4,1) = -0.0875 -1.2686 -0.8755 -0.9495 -0.2446 1.3006 1.4207 0.1327 -0.4955 0.1436 -1.2012 -0.7976 0.4591 0.8676 -0.5111 A1(:,:,5,1) = -1.2929 1.0890 1.9986 -1.2038 1.7113 -0.5439 0.4822 -0.9713 -0.7344 -2.7283 -0.2412 -1.5646 0.3954 0.3932 0.0305 A1(:,:,1,2) = -0.8256 0.5676 0.8116 0.1928 1.9774 -0.1392 -0.9322 -0.6886 -0.2211 -0.9856 -1.1456 -1.8686 -1.7883 -0.8405 -0.8021 A1(:,:,2,2) = -1.2553 -0.4216 -0.2697 0.4411 0.8144 1.4442 0.2413 2.6683 1.1350 -1.3589 -0.3461 -0.7527 1.2070 -1.4111 -1.5795 A1(:,:,3,2) = -0.2793 -0.5773 0.5650 0.6136 0.0992 0.2808 -0.4035 -0.4519 -0.9614 0.4319 -0.8908 0.8141 1.3367 0.4238 -1.5608 A1(:,:,4,2) = -0.1779 1.2839 0.0125 2.2352 -0.9035 -0.7614 -0.5095 0.6968 0.1772 0.1354 -0.0950 0.4881 -1.0030 -0.1946 -0.8392 A1(:,:,5,2) = 0.6141 2.8421 -0.5669 0.2201 -0.1253 0.5792 -0.3322 2.1357 -1.9412 0.3375 0.0497 -0.7014 0.1421 -0.4291 -0.6321 A1(:,:,1,3) = 1.1489 -1.1807 -0.4492 -1.1759 0.1888 0.2444 -1.0774 -0.4612 -0.6100 -1.6502 -1.4490 -0.4873 -0.3277 1.1480 0.2517 A1(:,:,2,3) = -0.9062 0.6402 0.5852 -1.6361 1.2370 -0.7590 -0.7849 -1.1540 0.6493 0.8035 0.0221 0.1192 0.8192 -0.3099 0.9330 A1(:,:,3,3) = 0.6155 -0.3603 -2.6079 0.2124 -0.7502 0.7548 0.4900 1.9064 -1.1689 1.1729 1.2231 0.1986 -1.5402 0.4737 -1.2950 A1(:,:,4,3) = 0.6996 2.4545 -0.1478 -0.8243 -0.1037 -0.5947 -1.0549 -0.3369 0.1809 -0.1429 -1.7245 -0.2607 0.0765 0.3593 -0.6157 A1(:,:,5,3) = -0.6423 -0.9207 0.4741 -0.1223 -0.5415 -0.2316 -0.5562 -0.6291 -1.2746 0.4366 -1.4189 -0.5076 0.9871 1.5609 0.9176 A1(:,:,1,4) = -0.4445 0.5182 -0.0032 -1.5960 0.5054 -0.4360 -0.5551 -1.1994 0.8797 0.3545 0.4219 -1.4632 2.5022 -0.1467 0.5085 A1(:,:,2,4) = -0.4816 -0.8249 -0.5752 -0.7183 -1.0844 -0.2513 0.2733 -1.4182 0.1800 -0.8756 -2.6553 0.2466 2.1158 1.2658 -1.5856 A1(:,:,3,4) = -0.8998 0.7047 -0.2158 0.6237 -0.9447 1.1929 1.3013 1.3105 -1.5414 0.2255 -0.4269 0.9128 -0.5521 -1.6838 1.4127 A1(:,:,4,4) = -0.4268 -1.2089 1.5140 0.3832 1.3052 0.3509 -0.1716 0.4853 -0.3469 -0.5305 1.3863 0.7404 1.4151 0.7570 1.1887 A1(:,:,5,4) = 0.2859 -0.6849 0.1297 -0.7941 -0.1797 -0.6208 -0.3502 -0.3451 -1.1465 -0.5422 0.0403 -0.0338 1.6394 -1.4634 0.3860 A1(:,:,1,5) = 0.5958 0.3162 1.9014 0.6868 -0.1893 -0.2849 -0.7839 -0.0832 -1.5166 1.0663 -0.4493 0.1315 0.1884 -0.4110 -0.8126 A1(:,:,2,5) = -1.2354 0.6969 0.0894 0.4473 1.3918 -1.3063 -0.3731 -0.0608 2.9289 -0.4286 -2.3352 -0.1409 2.0944 -0.1253 0.1860 A1(:,:,3,5) = -0.1012 1.8440 0.2451 0.5655 -0.9241 -0.4988 0.4886 1.6567 0.1714 0.5465 1.7825 0.9002 -0.8090 0.6447 -0.3383 A1(:,:,4,5) = 0.1459 -0.4494 -1.9356 1.4918 -1.1182 -1.6562 1.2623 -0.7573 -0.3625 0.4339 -1.2187 -0.4862 0.5742 0.6610 -0.2768 A1(:,:,5,5) = -1.1884 -0.4624 0.5660 -0.1210 0.8187 -0.0676 -0.8684 0.0765 1.1045 -0.0897 0.5259 -0.1595 0.4730 1.1096 0.5220 A2 = Columns 1 through 17 -0.0616 -1.0761 0.5527 -0.0084 2.0803 -0.7017 -1.1692 0.8001 -1.6007 2.1820 0.7921 0.7887 0.2248 0.2636 -0.3587 -1.2012 -0.7976 Columns 18 through 34 0.4591 0.8676 -0.5111 -0.2412 -1.5646 0.3954 0.3932 0.0305 -1.1456 -1.8686 -1.7883 -0.8405 -0.8021 -0.3461 -0.7527 1.2070 -1.4111 Columns 35 through 51 -1.5795 -0.8908 0.8141 1.3367 0.4238 -1.5608 -0.0950 0.4881 -1.0030 -0.1946 -0.8392 0.0497 -0.7014 0.1421 -0.4291 -0.6321 -1.4490 Columns 52 through 68 -0.4873 -0.3277 1.1480 0.2517 0.0221 0.1192 0.8192 -0.3099 0.9330 1.2231 0.1986 -1.5402 0.4737 -1.2950 -1.7245 -0.2607 0.0765 Columns 69 through 85 0.3593 -0.6157 -1.4189 -0.5076 0.9871 1.5609 0.9176 0.4219 -1.4632 2.5022 -0.1467 0.5085 -2.6553 0.2466 2.1158 1.2658 -1.5856 Columns 86 through 102 -0.4269 0.9128 -0.5521 -1.6838 1.4127 1.3863 0.7404 1.4151 0.7570 1.1887 0.0403 -0.0338 1.6394 -1.4634 0.3860 -0.4493 0.1315 Columns 103 through 119 0.1884 -0.4110 -0.8126 -2.3352 -0.1409 2.0944 -0.1253 0.1860 1.7825 0.9002 -0.8090 0.6447 -0.3383 -1.2187 -0.4862 0.5742 0.6610 Columns 120 through 125 -0.2768 0.5259 -0.1595 0.4730 1.1096 0.5220 S(:,:,1) = -0.0616 -0.7017 0.7921 -1.2012 -0.2412 -1.0761 -1.1692 0.7887 -0.7976 -1.5646 0.5527 0.8001 0.2248 0.4591 0.3954 -0.0084 -1.6007 0.2636 0.8676 0.3932 2.0803 2.1820 -0.3587 -0.5111 0.0305 S(:,:,2) = -1.1456 -0.3461 -0.8908 -0.0950 0.0497 -1.8686 -0.7527 0.8141 0.4881 -0.7014 -1.7883 1.2070 1.3367 -1.0030 0.1421 -0.8405 -1.4111 0.4238 -0.1946 -0.4291 -0.8021 -1.5795 -1.5608 -0.8392 -0.6321 S(:,:,3) = -1.4490 0.0221 1.2231 -1.7245 -1.4189 -0.4873 0.1192 0.1986 -0.2607 -0.5076 -0.3277 0.8192 -1.5402 0.0765 0.9871 1.1480 -0.3099 0.4737 0.3593 1.5609 0.2517 0.9330 -1.2950 -0.6157 0.9176 S(:,:,4) = 0.4219 -2.6553 -0.4269 1.3863 0.0403 -1.4632 0.2466 0.9128 0.7404 -0.0338 2.5022 2.1158 -0.5521 1.4151 1.6394 -0.1467 1.2658 -1.6838 0.7570 -1.4634 0.5085 -1.5856 1.4127 1.1887 0.3860 S(:,:,5) = -0.4493 -2.3352 1.7825 -1.2187 0.5259 0.1315 -0.1409 0.9002 -0.4862 -0.1595 0.1884 2.0944 -0.8090 0.5742 0.4730 -0.4110 -0.1253 0.6447 0.6610 1.1096 -0.8126 0.1860 -0.3383 -0.2768 0.5220

5   Pass
A = randn(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2); assert(isequal(arraySlice(A,2,18),A(:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,2)))

ns = 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 A1 = 1.8337 -0.1122 A2 = -0.1122 S = -0.1122

### Community Treasure Hunt

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

Start Hunting!