Cody

# Problem 375. N-Dimensional Array Slice

Solution 1837265

Submitted on 4 Jun 2019 by John Walker
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)))

A = -0.4418 0.9487 1.0356 -0.8841 -0.2854 -0.2894 -0.7401 -0.4590 1.2387 0.4345 -0.2810 -0.5105 -0.8778 -1.0674 0.3390 -0.6639 0.8560 -0.6085 0.1420 -0.4098 -0.7128 1.0266 -0.0475 0.5503 -0.5325 ord = 2 1 ans = -0.4418 -0.2894 -0.2810 -0.6639 -0.7128 0.9487 -0.7401 -0.5105 0.8560 1.0266 1.0356 -0.4590 -0.8778 -0.6085 -0.0475 -0.8841 1.2387 -1.0674 0.1420 0.5503 -0.2854 0.4345 0.3390 -0.4098 -0.5325 ans = -0.8841 1.2387 -1.0674 0.1420 0.5503

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

A = 1.0398 0.4979 1.7023 -0.9058 -0.5265 -1.1058 1.5725 -0.6492 -1.1152 1.0868 -1.3923 -0.5270 -1.2895 1.7690 -1.3089 -1.7496 1.3087 1.1800 0.3084 -0.8755 0.3875 -0.6350 -1.8675 0.3321 0.2723 ord = 1 2 ans = 1.0398 0.4979 1.7023 -0.9058 -0.5265 -1.1058 1.5725 -0.6492 -1.1152 1.0868 -1.3923 -0.5270 -1.2895 1.7690 -1.3089 -1.7496 1.3087 1.1800 0.3084 -0.8755 0.3875 -0.6350 -1.8675 0.3321 0.2723 ans = -1.7496 1.3087 1.1800 0.3084 -0.8755

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

A = -0.0530 1.1557 -2.2530 -1.3052 -0.3748 -2.0665 -1.1219 0.6356 0.2027 -0.1037 0.5994 -1.1753 -2.0894 0.1357 2.4724 0.5270 0.0293 -0.3987 -0.1124 -0.7382 1.0857 0.4635 1.1442 0.1078 -0.1021 ord = 10 1 2 3 4 5 6 7 8 9 ans(:,:,1) = -0.0530 -2.0665 0.5994 0.5270 1.0857 ans(:,:,2) = 1.1557 -1.1219 -1.1753 0.0293 0.4635 ans(:,:,3) = -2.2530 0.6356 -2.0894 -0.3987 1.1442 ans(:,:,4) = -1.3052 0.2027 0.1357 -0.1124 0.1078 ans(:,:,5) = -0.3748 -0.1037 2.4724 -0.7382 -0.1021 ans = -0.0530 1.1557 -2.2530 -1.3052 -0.3748 -2.0665 -1.1219 0.6356 0.2027 -0.1037 0.5994 -1.1753 -2.0894 0.1357 2.4724 0.5270 0.0293 -0.3987 -0.1124 -0.7382 1.0857 0.4635 1.1442 0.1078 -0.1021

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

A(:,:,1,1) = 1.1094 -1.2174 1.9158 0.5230 -0.9411 0.0865 -0.3123 -1.7292 0.9045 -0.0427 -3.6012 0.6655 -0.8910 0.3921 0.2897 1.2310 -1.1357 0.2070 -0.5747 1.0179 -1.5215 -0.2133 -0.6333 -0.8297 -0.4386 A(:,:,2,1) = -0.2200 0.5800 -0.9366 1.1510 -0.7751 0.2897 -0.6619 0.4180 -0.9208 0.2821 -0.4629 -0.1207 0.2008 0.2530 0.1726 -0.0734 -0.5587 -1.1042 2.0995 -0.7657 2.5745 1.2142 0.0768 1.8042 -0.1539 A(:,:,3,1) = -0.4414 -1.1528 0.1524 0.0691 -0.0415 -0.1228 -0.6517 -1.0134 -0.3287 0.1437 1.0004 -0.4982 -0.5077 1.5014 1.2673 0.6089 -0.6703 -1.4366 0.1345 -1.2806 2.7879 -0.3825 -0.9086 0.5771 -0.1888 A(:,:,4,1) = -0.2346 -0.1369 -0.4719 1.5755 -2.6760 0.4008 -0.4295 0.0583 0.5286 0.1801 1.3232 0.0112 0.9776 0.4763 0.9166 -0.1229 0.7366 0.7314 -0.8815 -1.0260 -0.4144 0.6891 1.1559 0.6625 0.0022 A(:,:,5,1) = -0.1764 0.9105 0.4824 -0.3764 0.0644 0.0080 -0.0625 -0.8752 -0.9094 0.8655 0.3916 0.0765 -0.7510 0.2201 -1.0530 -0.7944 0.1062 1.0801 -0.0341 -1.6783 0.5465 0.8696 -0.6676 -1.0379 -0.9398 A(:,:,1,2) = -2.6318 0.1008 -0.4901 -0.5310 1.7191 0.8871 0.0241 -0.5676 0.5760 -0.5093 0.0343 -2.8402 -0.3458 0.1676 -1.3415 1.2411 0.7455 -0.6755 -1.5705 0.3764 0.1272 1.1088 -0.2071 -0.4820 1.1561 A(:,:,2,2) = 0.7760 -0.5296 0.7963 0.5267 0.9383 0.1232 -0.5443 -1.0386 2.5967 0.2338 -1.3763 -0.6145 -0.8663 0.0984 1.6643 0.1900 0.7269 -0.4420 -0.5161 -1.1666 0.6553 1.4422 0.7750 -0.4320 -0.4242 A(:,:,3,2) = 0.1373 0.7708 -0.5952 0.2631 0.3071 -0.3880 1.1460 -1.1502 -1.7519 -1.2998 -0.2979 -1.0847 -0.7773 -0.8772 -0.9472 0.6247 -0.1027 -0.4537 -1.8716 1.5183 -0.4124 0.8226 0.4502 -2.1723 -1.0901 A(:,:,4,2) = 0.0580 -0.5289 -1.2642 0.5568 -0.0890 -0.3627 -0.4581 1.0915 0.6579 -1.4121 0.7469 -0.0476 -1.1844 0.7534 0.7922 -1.0091 -0.9478 -0.6499 0.9312 -0.6270 -1.9694 1.0869 -0.1982 -1.1605 -1.0545 A(:,:,5,2) = -0.0356 0.8889 -1.2752 2.6525 0.9075 -0.2446 0.6015 0.5866 -0.3270 -1.0712 -0.1347 0.0376 0.9008 -1.0773 -0.7271 0.4722 -1.3510 -0.4580 1.6382 -1.0482 -0.2065 -1.1762 1.1731 -1.1817 0.3971 A(:,:,1,3) = -0.1037 -1.1700 1.2594 1.3692 0.8607 0.2287 -1.8434 1.3326 -0.4866 0.7715 -1.6164 -0.9519 -1.1446 0.7528 -0.3985 1.3351 0.7031 0.2960 -0.1101 0.3433 0.3113 -0.6288 -0.5148 0.2073 2.5375 A(:,:,2,3) = -1.1552 0.6151 0.5105 -0.0979 -0.7944 0.2105 1.4225 0.5918 0.6001 0.7631 0.1565 1.5977 -0.0343 0.3244 0.3340 0.6753 -0.9041 -1.0840 0.5344 -0.2892 0.6513 1.3343 0.2756 -0.3095 -1.1879 A(:,:,3,3) = -0.3858 -1.2469 0.2920 -2.0890 -0.7053 -0.6210 0.2758 1.4688 -1.5409 -0.7106 -2.1370 1.1234 -1.1796 0.2042 0.8322 2.0967 0.4311 0.8103 -0.3579 -2.1822 -1.0823 0.0402 0.1876 2.1196 1.4124 A(:,:,4,3) = -1.0432 0.2381 -0.7586 1.7965 0.2765 -0.5031 -0.0921 -0.1190 2.4942 1.4075 1.2275 0.4331 -0.6506 0.6755 1.3473 0.9247 -0.6211 -1.1902 0.4350 -0.6532 -0.1110 -0.0675 0.0632 -0.3552 -1.1706 A(:,:,5,3) = 0.6112 -1.4317 1.2146 -1.7380 -0.9562 0.4896 -0.8333 1.9997 1.2697 -0.4417 -0.0219 -1.3770 0.0860 0.9898 2.3449 0.2894 -0.3841 0.0931 0.6256 0.2205 -0.1454 -0.7062 -1.6106 1.5112 -0.1034 ord = 4 1 2 3 ans(:,:,1,1) = 1.1094 0.0865 -3.6012 1.2310 -1.5215 -2.6318 0.8871 0.0343 1.2411 0.1272 -0.1037 0.2287 -1.6164 1.3351 0.3113 ans(:,:,2,1) = -1.2174 -0.3123 0.6655 -1.1357 -0.2133 0.1008 0.0241 -2.8402 0.7455 1.1088 -1.1700 -1.8434 -0.9519 0.7031 -0.6288 ans(:,:,3,1) = 1.9158 -1.7292 -0.8910 0.2070 -0.6333 -0.4901 -0.5676 -0.3458 -0.6755 -0.2071 1.2594 1.3326 -1.1446 0.2960 -0.5148 ans(:,:,4,1) = 0.5230 0.9045 0.3921 -0.5747 -0.8297 -0.5310 0.5760 0.1676 -1.5705 -0.4820 1.3692 -0.4866 0.7528 -0.1101 0.2073 ans(:,:,5,1) = -0.9411 -0.0427 0.2897 1.0179 -0.4386 1.7191 -0.5093 -1.3415 0.3764 1.1561 0.8607 0.7715 -0.3985 0.3433 2.5375 ans(:,:,1,2) = -0.2200 0.2897 -0.4629 -0.0734 2.5745 0.7760 0.1232 -1.3763 0.1900 0.6553 -1.1552 0.2105 0.1565 0.6753 0.6513 ans(:,:,2,2) = 0.5800 -0.6619 -0.1207 -0.5587 1.2142 -0.5296 -0.5443 -0.6145 0.7269 1.4422 0.6151 1.4225 1.5977 -0.9041 1.3343 ans(:,:,3,2) = -0.9366 0.4180 0.2008 -1.1042 0.0768 0.7963 -1.0386 -0.8663 -0.4420 0.7750 0.5105 0.5918 -0.0343 -1.0840 0.2756 ans(:,:,4,2) = 1.1510 -0.9208 0.2530 2.0995 1.8042 0.5267 2.5967 0.0984 -0.5161 -0.4320 -0.0979 0.6001 0.3244 0.5344 -0.3095 ans(:,:,5,2) = -0.7751 0.2821 0.1726 -0.7657 -0.1539 0.9383 0.2338 1.6643 -1.1666 -0.4242 -0.7944 0.7631 0.3340 -0.2892 -1.1879 ans(:,:,1,3) = -0.4414 -0.1228 1.0004 0.6089 2.7879 0.1373 -0.3880 -0.2979 0.6247 -0.4124 -0.3858 -0.6210 -2.1370 2.0967 -1.0823 ans(:,:,2,3) = -1.1528 -0.6517 -0.4982 -0.6703 -0.3825 0.7708 1.1460 -1.0847 -0.1027 0.8226 -1.2469 0.2758 1.1234 0.4311 0.0402 ans(:,:,3,3) = 0.1524 -1.0134 -0.5077 -1.4366 -0.9086 -0.5952 -1.1502 -0.7773 -0.4537 0.4502 0.2920 1.4688 -1.1796 0.8103 0.1876 ans(:,:,4,3) = 0.0691 -0.3287 1.5014 0.1345 0.5771 0.2631 -1.7519 -0.8772 -1.8716 -2.1723 -2.0890 -1.5409 0.2042 -0.3579 2.1196 ans(:,:,5,3) = -0.0415 0.1437 1.2673 -1.2806 -0.1888 0.3071 -1.2998 -0.9472 1.5183 -1.0901 -0.7053 -0.7106 0.8322 -2.1822 1.4124 ans(:,:,1,4) = -0.2346 0.4008 1.3232 -0.1229 -0.4144 0.0580 -0.3627 0.7469 -1.0091 -1.9694 -1.0432 -0.5031 1.2275 0.9247 -0.1110 ans(:,:,2,4) = -0.1369 -0.4295 0.0112 0.7366 0.6891 -0.5289 -0.4581 -0.0476 -0.9478 1.0869 0.2381 -0.0921 0.4331 -0.6211 -0.0675 ans(:,:,3,4) = -0.4719 0.0583 0.9776 0.7314 1.1559 -1.2642 1.0915 -1.1844 -0.6499 -0.1982 -0.7586 -0.1190 -0.6506 -1.1902 0.0632 ans(:,:,4,4) = 1.5755 0.5286 0.4763 -0.8815 0.6625 0.5568 0.6579 0.7534 0.9312 -1.1605 1.7965 2.4942 0.6755 0.4350 -0.3552 ans(:,:,5,4) = -2.6760 0.1801 0.9166 -1.0260 0.0022 -0.0890 -1.4121 0.7922 -0.6270 -1.0545 0.2765 1.4075 1.3473 -0.6532 -1.1706 ans(:,:,1,5) = -0.1764 0.0080 0.3916 -0.7944 0.5465 -0.0356 -0.2446 -0.1347 0.4722 -0.2065 0.6112 0.4896 -0.0219 0.2894 -0.1454 ans(:,:,2,5) = 0.9105 -0.0625 0.0765 0.1062 0.8696 0.8889 0.6015 0.0376 -1.3510 -1.1762 -1.4317 -0.8333 -1.3770 -0.3841 -0.7062 ans(:,:,3,5) = 0.4824 -0.8752 -0.7510 1.0801 -0.6676 -1.2752 0.5866 0.9008 -0.4580 1.1731 1.2146 1.9997 0.0860 0.0931 -1.6106 ans(:,:,4,5) = -0.3764 -0.9094 0.2201 -0.0341 -1.0379 2.6525 -0.3270 -1.0773 1.6382 -1.1817 -1.7380 1.2697 0.9898 0.6256 1.5112 ans(:,:,5,5) = 0.0644 0.8655 -1.0530 -1.6783 -0.9398 0.9075 -1.0712 -0.7271 -1.0482 0.3971 -0.9562 -0.4417 2.3449 0.2205 -0.1034 ans(:,:,1) = -0.1037 -1.1700 1.2594 1.3692 0.8607 0.2287 -1.8434 1.3326 -0.4866 0.7715 -1.6164 -0.9519 -1.1446 0.7528 -0.3985 1.3351 0.7031 0.2960 -0.1101 0.3433 0.3113 -0.6288 -0.5148 0.2073 2.5375 ans(:,:,2) = -1.1552 0.6151 0.5105 -0.0979 -0.7944 0.2105 1.4225 0.5918 0.6001 0.7631 0.1565 1.5977 -0.0343 0.3244 0.3340 0.6753 -0.9041 -1.0840 0.5344 -0.2892 0.6513 1.3343 0.2756 -0.3095 -1.1879 ans(:,:,3) = -0.3858 -1.2469 0.2920 -2.0890 -0.7053 -0.6210 0.2758 1.4688 -1.5409 -0.7106 -2.1370 1.1234 -1.1796 0.2042 0.8322 2.0967 0.4311 0.8103 -0.3579 -2.1822 -1.0823 0.0402 0.1876 2.1196 1.4124 ans(:,:,4) = -1.0432 0.2381 -0.7586 1.7965 0.2765 -0.5031 -0.0921 -0.1190 2.4942 1.4075 1.2275 0.4331 -0.6506 0.6755 1.3473 0.9247 -0.6211 -1.1902 0.4350 -0.6532 -0.1110 -0.0675 0.0632 -0.3552 -1.1706 ans(:,:,5) = 0.6112 -1.4317 1.2146 -1.7380 -0.9562 0.4896 -0.8333 1.9997 1.2697 -0.4417 -0.0219 -1.3770 0.0860 0.9898 2.3449 0.2894 -0.3841 0.0931 0.6256 0.2205 -0.1454 -0.7062 -1.6106 1.5112 -0.1034

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

A(:,:,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1) = -1.4854 A(:,:,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2) = 1.7721 ord = 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ans = -1.4854 1.7721 ans = 1.7721

### Community Treasure Hunt

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

Start Hunting!