# 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.

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 };
//will be a column vector
//with non-zeros only in rows
//marked for deletion

//Delete rows and assign new matrix to 'xout'

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);
//Create a vector with 1's in
//rows marked for deletion
//and zeros everywhere else

//Remove the rows and return 'xout'
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.

### Have a Specific Question?

Get a real answer from a real person

### Need Support?

Get help from our friendly experts.