Cody

Problem 375. N-Dimensional Array Slice

Solution 2954884

Submitted on 16 Sep 2020
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)))

list = 1 2 3 4 5 listnotI = 1 2 3 5 listd = 1 2 listdnotd = 1 S = struct with fields: subs: {':' [1 2 3 5]} S = 0.4813 0.7035 0.0918 -0.3322 -0.4913

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

list = 1 2 3 4 5 listnotI = 1 2 3 5 listd = 1 2 listdnotd = 2 S = struct with fields: subs: {[1 2 3 5] ':'} S = 0.2802 0.6182 -0.6217 -1.4275 -0.6177

Assertion failed.

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

list = 1 listnotI = [] listd = 1 2 listdnotd = 1 2 S = struct with fields: subs: {':' ':' [] [] [] [] [] [] [] []}

A null assignment can have only one non-colon index. Error in arraySlice (line 18) B = subsasgn(A,S,[]); Error in Test3 (line 2) assert(isequal(arraySlice(A,1,10),A))

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

list = 1 2 3 listnotI = 1 2 listd = 1 2 3 4 listdnotd = 1 2 3 S = struct with fields: subs: {':' ':' ':' [1 2]} S(:,:,1) = 0.5879 -0.5516 -0.5332 -0.4955 0.1626 0.3090 0.1630 -0.2859 0.7340 0.2889 -1.0058 0.9482 1.4747 1.5689 0.5002 0.6203 1.5727 1.0595 -0.0226 1.2755 -0.2802 0.9951 -0.3907 0.3437 1.5766 S(:,:,2) = -0.2089 -1.2845 -0.5140 -0.0475 -0.4132 -0.5323 0.6276 1.5567 1.6482 0.0181 0.4254 -0.2516 -0.6807 -0.9817 1.0348 -0.5519 0.0499 1.3229 -0.8011 1.4662 -0.0265 -0.3807 0.6941 3.3122 0.2814 S(:,:,3) = -0.0180 2.1689 0.6439 1.2434 1.6972 0.1741 -0.8746 -0.1615 0.4320 -0.5275 -1.2932 -1.4277 0.9671 0.9652 0.7676 -1.7833 -0.1065 -0.5036 -0.7776 1.0309 -0.1513 -0.4535 1.5502 -0.7670 -0.7637 S(:,:,4) = -0.1243 0.8664 0.8480 0.0521 -1.2346 0.3439 -2.2163 -0.3913 0.3412 0.8204 0.9303 0.7739 0.7467 -0.6490 -0.4830 1.6033 -1.0758 1.5367 -0.2205 0.4723 0.2582 0.1997 1.6003 1.0390 1.9874 S(:,:,5) = 0.4051 -1.0060 0.4899 0.2893 1.2045 0.3692 -0.7163 0.2819 2.7863 0.1029 -0.6761 0.6738 -1.2745 -0.4673 -0.0347 0.1625 1.1171 0.9356 -0.4860 -1.8070 -0.3819 -0.0253 -0.3485 -0.8329 1.3377

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

list = 1 2 listnotI = 1 listd = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 listdnotd = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 S = struct with fields: subs: {':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' ':' [1]} S = -0.8389

Suggested Problems

More from this Author56

Community Treasure Hunt

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

Start Hunting!