Cody

Problem 796. Removing rows from a matrix is easy - but what about inserting rows?

Solution 2733346

Submitted on 23 Jul 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 = 1; IND = 1; b = 1; y_correct = [1;1]; assert(isequal(insertRows(A, IND, b), y_correct))

m = 1 n = 1 o = 1 k = 0 k = 1 y = 1 k = 2 y = 1 1

2   Fail
A = [0 0; 1 1]; IND = [1 1 1 2]; b = NaN; y_correct = [0 0;NaN NaN;NaN NaN;NaN NaN;1 1;NaN NaN]; assert(isequalwithequalnans(insertRows(A, IND, b), y_correct))

m = 2 n = 2 o = 4 k = 0 k = 1 y = 0 0 k = 2 y = 0 0 NaN NaN k = 3

Index exceeds the number of array elements (1). Error in insertRows (line 13) y(k,:)= b(j) Error in Test2 (line 5) assert(isequalwithequalnans(insertRows(A, IND, b), y_correct))

3   Pass
A = [1 1; 3 3; 4 4]; IND = [1 3]; b = [2 2; 5 5]; y_correct = [1 1;2 2;3 3;4 4;5 5]; assert(isequal(insertRows(A, IND, b), y_correct))

m = 3 n = 2 o = 2 k = 0 k = 1 y = 1 1 k = 2 y = 1 1 2 2 k = 3 y = 1 1 2 2 3 3 k = 4 y = 1 1 2 2 3 3 4 4 k = 5 y = 1 1 2 2 3 3 4 4 5 5

4   Pass
A = (1:2:10)'; IND = 1:5; b = (2:2:10)'; y_correct = (1:10)'; assert(isequal(insertRows(A, IND, b), y_correct))

m = 5 n = 1 o = 5 k = 0 k = 1 y = 1 k = 2 y = 1 2 k = 3 y = 1 2 3 k = 4 y = 1 2 3 4 k = 5 y = 1 2 3 4 5 k = 6 y = 1 2 3 4 5 6 k = 7 y = 1 2 3 4 5 6 7 k = 8 y = 1 2 3 4 5 6 7 8 k = 9 y = 1 2 3 4 5 6 7 8 9 k = 10 y = 1 2 3 4 5 6 7 8 9 10

5   Fail
A = zeros(10,5); IND = [3,7]; b = rand(2,5); y_correct = [zeros(3,5); b(1,:); zeros(4,5); b(2,:); zeros(3,5)]; assert(isequal(insertRows(A, IND, b), y_correct))

m = 10 n = 5 o = 2 k = 0 k = 1 y = 0 0 0 0 0 k = 2 y = 0 0 0 0 0 0 0 0 0 0 k = 3 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 k = 4 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 k = 5 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 k = 6 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 k = 7 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 k = 8 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 k = 9 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5891 0.5891 0.5891 0.5891 0.5891 k = 10 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5891 0.5891 0.5891 0.5891 0.5891 0 0 0 0 0 k = 11 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5891 0.5891 0.5891 0.5891 0.5891 0 0 0 0 0 0 0 0 0 0 k = 12 y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5419 0.5419 0.5419 0.5419 0.5419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5891 0.5891 0.5891 0.5891 0.5891 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Assertion failed.

6   Pass
assert(isempty(strfind(evalc('type insertRows'), 'regexp')));