Cody

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

Solution 2742109

Submitted on 26 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))

I = 1 b = 1 A = 1 1

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

I = 2 1 1 1 b = NaN b = NaN NaN NaN NaN A = 0 0 1 1 NaN NaN A = 0 0 NaN NaN 1 1 NaN NaN A = 0 0 NaN NaN NaN NaN 1 1 NaN NaN A = 0 0 NaN NaN NaN NaN NaN NaN 1 1 NaN NaN

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

I = 3 1 b = 5 5 2 2

Index in position 1 exceeds array bounds (must not exceed 2). Error in insertRows (line 8) A= [A(1:I(k),:); b(I(k),:); A((I(k)+1):end,:)] Error in Test3 (line 5) assert(isequal(insertRows(A, IND, b), y_correct))

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

I = 5 4 3 2 1 b = 10 8 6 4 2 A = 1 3 5 7 9 2 A = 1 3 5 7 4 9 2 A = 1 3 5 6 7 4 9 2 A = 1 3 8 5 6 7 4 9 2 A = 1 10 3 8 5 6 7 4 9 2

Assertion failed.

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

I = 7 3 b = 0.6967 0.7507 0.6048 0.1852 0.0264 0.1773 0.6723 0.1369 0.7355 0.5295

Index in position 1 exceeds array bounds (must not exceed 2). Error in insertRows (line 8) A= [A(1:I(k),:); b(I(k),:); A((I(k)+1):end,:)] Error in Test5 (line 5) assert(isequal(insertRows(A, IND, b), y_correct))

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

Suggested Problems

More from this Author1

Community Treasure Hunt

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

Start Hunting!