Matlab

How to Delete Rows in MATLAB

MATLAB supports many row and column operations that are useful in matrix theory. One of these operations is deleting a row or rows from the matrix. A row can be easily deleted from a matrix by setting it equal to two empty square brackets []. Using this article, we will learn how to delete a row from the matrix using some examples.

How to Delete Rows from a Matrix in MATLAB

We can delete a single row, multiple rows, or all rows from the matrix by using the given syntax.

A(m, :)
A(m: r, :)
A([m1, m2, m3,,,mn], :)
A(1:m, :)

 

Here:

  • A(m, 🙂 yields a way to delete a single row by specifying the row number which is m. Replace the m with an integral value row number you need to remove.
  • A(m: r, 🙂 yields a way to delete multiple consecutive rows by specifying the range m:r where m is the first row and r is the last row of the specified range.
  • A([m1, m2, m3…mn], 🙂 yields a way to delete multiple rows that are not in a sequence by specifying the row numbers in square brackets.
  • A(1:m, 🙂 yields a way to delete all rows by specifying the range from 1:m. Where 1 is the first row and m is the last row of the given matrix.

Example 1

In this example, we demonstrate how to delete a single row from the given matrix in MATLAB. To perform this operation, first, we create a matrix having 4 rows and 7 columns using the rand() function that generates all random numbers as a matrix element. After that, we delete the second row of the matrix by mentioning its row number. Then the resultant matrix is displayed on the screen.

A= rand(4, 7)
A(2, :)=[]

 

Example 2

In this example, we demonstrate how to delete multiple rows from the given matrix in MATLAB. To perform this operation, first, we create a matrix having 4 rows and 7 columns using the rand() function that generates all random numbers as a matrix element. After that, we delete the matrix rows by mentioning the range of row numbers. Then the resultant matrix is displayed on the screen.

A= rand(4,7)
A(2:4, :)=[]

 

Example 3

In this example, we demonstrate how to delete multiple rows that are not consecutive from the given matrix in MATLAB. To perform this operation, first, we create a matrix having 4 rows and 7 columns using the rand() function that generates all random numbers as a matrix element. Then, we delete the matrix rows by mentioning the row number in square brackets. Then the resultant matrix is displayed on the screen.

A= rand(4, 7)
A([2, 4], :)=[]

 

Conclusion

MATLAB supports many row and column operations that are helpful in matrix theory. One of these operations is deleting a row or rows from the matrix. A row can be easily deleted from a matrix by setting it equal to two empty square brackets []. This tutorial provided us with different ways to delete single or multiple rows from the matrix.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.