delete rows of a matrix

How to delete several rows of a matrix with a simple command? Suppose I want to delete rows 2, 5, 7 of a matrix.

3 Answers



0



How to delete selected rows of a matrix will depend upon why you would like to delete the rows. If you want to delete rows that match a specific logical expression, you would use the delif command. For example if you wanted to remove all rows in which there was a negative element, you could do this:

x = { -1 0,
       2 3,
       3 1,
      -4 2,
       5 9,
       6 1,
      -7 2 };
//Sum the rows so 'mask'
//will be a column vector
//with non-zeros only in rows
//marked for deletion
mask = sumr(a .< 0));

//Delete rows and assign new matrix to 'xout'
xout = delif(x, mask);

After the code above xout will equal:

  
         2   3 
xout =   3   1 
         5   9 
         6   1

If you just want to delete the rows, then you could use this small procedure below.

//Create an additive vector from 1-10
x = seqa(1, 1, 10);

//Rows to remove from 'x'
rem = { 2, 5, 7 };
xout = delRow(x, rem);

proc (1) = delRow(x, remove);
   local mask, xout;
   //Create a vector with 1's in
   //rows marked for deletion
   //and zeros everywhere else
   mask = zeros(rows(x), 1);
   mask[remove] = ones(rows(remove), 1);

   //Remove the rows and return 'xout'
   xout = delif(x, mask);
   retp(xout);
endp;

After this code above:

      1           1
      2           3
      3           4
x =   4   xout =  6
      5           8
      6           9
      7          10
      8 
      9 
     10 


0



I was thinking about whether there is a built-in simple command. For example, indx={2,5,7}, which collects the indices of rows to be deleted from matrix A. In Matlab, this can be simply done by "A(indx,:)=[]."  The procedure provided by using "delif" is ok (and I was using "delif", but I hate the fact that such a simple thing should call a procedure) ; I was looking for an efficient and elegant way of achieving this.



0



Your request will be passed on to development.

Your Answer

3 Answers

0

How to delete selected rows of a matrix will depend upon why you would like to delete the rows. If you want to delete rows that match a specific logical expression, you would use the delif command. For example if you wanted to remove all rows in which there was a negative element, you could do this:

x = { -1 0,
       2 3,
       3 1,
      -4 2,
       5 9,
       6 1,
      -7 2 };
//Sum the rows so 'mask'
//will be a column vector
//with non-zeros only in rows
//marked for deletion
mask = sumr(a .< 0));

//Delete rows and assign new matrix to 'xout'
xout = delif(x, mask);

After the code above xout will equal:

  
         2   3 
xout =   3   1 
         5   9 
         6   1

If you just want to delete the rows, then you could use this small procedure below.

//Create an additive vector from 1-10
x = seqa(1, 1, 10);

//Rows to remove from 'x'
rem = { 2, 5, 7 };
xout = delRow(x, rem);

proc (1) = delRow(x, remove);
   local mask, xout;
   //Create a vector with 1's in
   //rows marked for deletion
   //and zeros everywhere else
   mask = zeros(rows(x), 1);
   mask[remove] = ones(rows(remove), 1);

   //Remove the rows and return 'xout'
   xout = delif(x, mask);
   retp(xout);
endp;

After this code above:

      1           1
      2           3
      3           4
x =   4   xout =  6
      5           8
      6           9
      7          10
      8 
      9 
     10 
0

I was thinking about whether there is a built-in simple command. For example, indx={2,5,7}, which collects the indices of rows to be deleted from matrix A. In Matlab, this can be simply done by "A(indx,:)=[]."  The procedure provided by using "delif" is ok (and I was using "delif", but I hate the fact that such a simple thing should call a procedure) ; I was looking for an efficient and elegant way of achieving this.

0

Your request will be passed on to development.


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.