Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

Created by Dirk Engel

Assume A is a 5-by-5 matrix. A([2,4],:) = [] is a quick way to remove rows 2 and 4. Can you find a quick way to insert rows into A?

You are given a n-by-m matrix A, a 1-by-p vector of indices IND, and either a scalar value (1-by-1) or a p-by-m matrix b to be inserted AFTER rows IND of A. In case of a scalar value it should fill the whole row. Note that IND may contain duplicate indices, which means that more than one row should be inserted after that specific row index.

For example:

   A = [1 1; 
        3 3; 
        4 4];
   IND = [1 3];
   b = [2 2; 
        5 5];

should become

   y = [1 1; 
        2 2; 
        3 3; 
        4 4; 
        5 5];

And

   A = [0 0; 
        1 1];
   IND = [1 1 2];
   b = NaN;

should become

   y = [0   0; 
        NaN NaN; 
        NaN NaN; 
        1 1; 
        NaN NaN];

Problem Group

Solution Statistics

29 correct solutions 22 incorrect solutions
Last solution submitted on Jun 18, 2014

Problem Comments