Cody

Problem 375. N-Dimensional Array Slice

Solution 3061963

Submitted on 4 Oct 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   Fail
A = randn(5,5); assert(isequal(arraySlice(A,4,2),A(:,4)))

dim = 5 1 S = 0.8486 -0.8984 0.6876 0.4447 -0.5217

Assertion failed.

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

dim = 1 5 S = 0.1057 -0.9797 -0.1815 0.2142 -0.5208

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

Index exceeds the number of array elements (2). Error in arraySlice (line 7) ind = floor(ids/prod(dim(1:(d-1)))) == I; Error in Test3 (line 2) assert(isequal(arraySlice(A,1,10),A))

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

dim = 5 5 5 1

Error using reshape Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension. Error in arraySlice (line 10) S = reshape(S,dim) Error in Test4 (line 2) assert(isequal(arraySlice(A,3,4),A(:,:,:,3)))

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

dim = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Error using reshape Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension. Error in arraySlice (line 10) S = reshape(S,dim) Error in Test5 (line 2) assert(isequal(arraySlice(A,2,18),A(:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,2)))

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!