Cody

Problem 375. N-Dimensional Array Slice

Solution 2954890

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]}

2   Pass
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] ':'}

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) S = 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]}

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]}

Community Treasure Hunt

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

Start Hunting!