Cody

# Problem 375. N-Dimensional Array Slice

Solution 2173411

Submitted on 24 Mar 2020 by Nikolaos Nikolaou
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)))

string = ':,4' string = '(?@A(:,4))' ans = -0.1386 -0.1867 -0.5017 1.1329 -0.5118 s = [] s = -0.1386 -0.1867 -0.5017 1.1329 -0.5118

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

string = '4,:' string = '(?@A(4,:))' ans = -0.4710 -0.0847 0.1754 -1.2334 -0.5018 s = [] s = -0.4710 -0.0847 0.1754 -1.2334 -0.5018

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

string = ':,1' string = '(?@A(:,1))' ans = -2.6991 -1.0869 0.9624 -0.1908 2.8127 s = [] s = -2.6991 -1.0869 0.9624 -0.1908 2.8127 s = -2.6991 -1.3080 0.4813 -0.1454 -0.4498 -1.0869 -0.7984 0.3803 1.7540 -0.5827 0.9624 0.3456 -0.6753 0.4913 0.3706 -0.1908 0.1320 -0.5010 0.7069 -1.6091 2.8127 1.0299 0.3717 -0.1215 -1.6594

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

string = ':,:,:,3' string = '(?@A(:,:,:,3))' ans(:,:,1) = 1.2252 -1.0201 0.0413 -1.5162 -0.8175 0.5678 -0.0624 -1.1751 -0.0958 -0.2017 0.0201 -0.8795 -0.3753 -0.7867 -0.5161 -2.1415 2.2762 -2.3504 0.1602 -0.5178 -0.5562 1.6454 0.6383 2.1006 0.7727 ans(:,:,2) = -1.7159 -1.0297 0.2308 -0.4995 -0.1677 0.9833 -0.0791 0.6040 0.5350 0.5384 -0.1412 -0.4248 0.6037 1.2821 1.7808 -1.4945 -0.8230 -1.4635 -2.2092 0.1405 0.6804 -0.4887 0.3037 2.4419 -1.2558 ans(:,:,3) = -0.0957 0.5175 0.7441 -0.2970 -0.2441 -1.0410 -1.4523 -0.2894 1.4138 -0.7338 0.7502 0.4768 -0.4483 -1.7200 -0.7328 -0.4891 1.5247 -1.6103 -1.3977 -0.6397 -0.5119 0.5647 1.1705 -1.6134 1.1544 ans(:,:,4) = 1.5875 0.1561 -0.7146 2.4714 -0.8944 1.1294 -1.8044 -1.0288 0.6413 -0.7028 -0.3430 0.3779 -1.3889 1.0820 1.7461 -0.5517 0.4628 -1.0767 0.2349 -0.3609 0.7832 0.9265 0.5122 -0.7757 -1.4597 ans(:,:,5) = -0.4232 -0.7671 -0.2330 -0.8864 0.6513 -1.0545 -0.7069 -0.3042 -0.6972 0.1500 1.4165 0.7988 -1.3386 0.3165 0.0173 -0.5655 -0.9250 -0.6897 -0.4672 1.1444 -1.3126 0.0377 -0.0614 -0.7368 -2.1165 s = [] s(:,:,1) = 1.2252 -1.0201 0.0413 -1.5162 -0.8175 0.5678 -0.0624 -1.1751 -0.0958 -0.2017 0.0201 -0.8795 -0.3753 -0.7867 -0.5161 -2.1415 2.2762 -2.3504 0.1602 -0.5178 -0.5562 1.6454 0.6383 2.1006 0.7727 s(:,:,2) = -1.7159 -1.0297 0.2308 -0.4995 -0.1677 0.9833 -0.0791 0.6040 0.5350 0.5384 -0.1412 -0.4248 0.6037 1.2821 1.7808 -1.4945 -0.8230 -1.4635 -2.2092 0.1405 0.6804 -0.4887 0.3037 2.4419 -1.2558 s(:,:,3) = -0.0957 0.5175 0.7441 -0.2970 -0.2441 -1.0410 -1.4523 -0.2894 1.4138 -0.7338 0.7502 0.4768 -0.4483 -1.7200 -0.7328 -0.4891 1.5247 -1.6103 -1.3977 -0.6397 -0.5119 0.5647 1.1705 -1.6134 1.1544 s(:,:,4) = 1.5875 0.1561 -0.7146 2.4714 -0.8944 1.1294 -1.8044 -1.0288 0.6413 -0.7028 -0.3430 0.3779 -1.3889 1.0820 1.7461 -0.5517 0.4628 -1.0767 0.2349 -0.3609 0.7832 0.9265 0.5122 -0.7757 -1.4597 s(:,:,5) = -0.4232 -0.7671 -0.2330 -0.8864 0.6513 -1.0545 -0.7069 -0.3042 -0.6972 0.1500 1.4165 0.7988 -1.3386 0.3165 0.0173 -0.5655 -0.9250 -0.6897 -0.4672 1.1444 -1.3126 0.0377 -0.0614 -0.7368 -2.1165

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

string = ':,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,2' string = '(?@A(:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,2))' ans = -0.3639 s = [] s = -0.3639

### Community Treasure Hunt

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

Start Hunting!