Cody

# Problem 589. Matrix indexing with two vectors of indices

Solution 1942175

Submitted on 19 Sep 2019
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
M = rand(4); a = [4 3 2 1]; b = [1 2 3 4]; v_correct = M([4 7 10 13]); assert(isequal(twoIndex(M,a,b),v_correct))

v = 0.2735 v = 0.2735 0.2305 v = 0.2735 0.2305 0.3992 v = 0.2735 0.2305 0.3992 0.0622

2   Pass
M = rand(4); a = [3 3 1 3]; b = [1 1 3 1]; v_correct = M([3 3 9 3]); assert(isequal(twoIndex(M,a,b),v_correct))

v = 0.4354 v = 0.4354 0.4354 v = 0.4354 0.4354 0.5939 v = 0.4354 0.4354 0.5939 0.4354

3   Fail
M = rand(100); [a,b] = size(M); v_correct = M(end); assert(isequal(twoIndex(M,a,b),v_correct))

v = 0.3651

Index exceeds the number of array elements (1). Error in twoIndex (line 4) v=[v M(a(k),b(k))] Error in Test3 (line 4) assert(isequal(twoIndex(M,a,b),v_correct))

4   Fail
M = rand(100); a = []; b = []; assert(isempty(twoIndex(M,a,b)))

Index exceeds the number of array elements (0). Error in twoIndex (line 4) v=[v M(a(k),b(k))] Error in Test4 (line 4) assert(isempty(twoIndex(M,a,b)))

5   Fail
M = 1; a = [1 1 1 1 1]; b = [1 1 1 1 1]; v_correct = [1 1 1 1 1]; assert(isequal(twoIndex(M,a,b),v_correct))

v = 1

Assertion failed.