**deleting a submatrix**from a matrix; that can be performed by assigning the number of rows and columns of a given matrix to the square brackets []. While performing this operation, MATLAB users face an error “

**Matrix Index is Out of Range for Deletion**“.

If you encounter such an error while working in MATLAB, follow this guide to learn how to fix it.

## Why Do We Get the Error “Matrix Index is Out of Range for Deletion” in MATLAB

Whenever we are required to delete a submatrix from a given matrix or some specific number of rows or columns of the given matrix, we first access the matrix elements that we want to delete using matrix indexing and then assign them to the **[ ] operator**. But if we specify the row number or column number that is greater than the matrix index bound, we get the error “**Matrix Index is Out of Range for Deletion**“.

## How to Fix Error “Matrix Index is Out of Range for Deletion” in MATLAB

As we discussed earlier, the error “**Matrix Index is Out of Range for Deletion**” occurred due to deleting a row or column that does not lie in the specified matrix. So, this error can be fixed by deleting the row or column of the given matrix that must lie in the specified matrix. Now, we will first generate the error “**Matrix Index is Out of Range for Deletion**” and then fix it in the given examples.

## Example 1: How to Fix “Matrix Index is Out of Range for Deletion” While Deleting Matrix Rows in MATLAB?

The below given MATLAB code creates a square matrix with 10 rows and 10 columns. After that, it initializes a variable i by assigning it value 5. Now it deletes a submatrix from the given matrix by mentioning row numbers as an expression. When we evaluate this expression for the specified value of variable i which is 5, we get the row number 12. The colon operator (:) indicates that we want to delete all columns of the specified row number. However, since the matrix has only 10 rows, row number 12 is out of range, resulting in an error message “**Matrix Index is Out of Range for Deletion**“.

i=5;

A((i*3)-3,:)=[]

Now, we can fix this error by mentioning the row number that lies in the matrix.

i=10;

A(1:i-3,:)=[]

In the above code, we delete the first 7 rows of the given matrix A.

## Example 2: How to Fix “Matrix Index is Out of Range for Deletion” While Deleting a Submatrix from a Given Matrix in MATLAB?

In this example, we create a square matrix with 10 rows as well as 10 columns. After that, we initialize a variable i by assigning it value 5. Now we delete a submatrix from the given matrix by mentioning column numbers as an expression. When we evaluate this expression for the specified value of variable i which is 5, we get the column numbers from 7 to 15. The matrix has a number of columns 10, but the resulting column number range is from 7 to 15 which does not lie in the given matrix, so we get an error **“Matrix Index is Out of Range for Deletion”**.

i = 5;

A(:,7:i*3)=[]

Now, we can fix this error by mentioning the column range that lies in the matrix.

i = 5;

A(:,7:i*2)=[]

In the above code, we delete the last 4 columns of the given matrix A.

## Conclusion

While performing different matrix operations in MATLAB, we get errors due to technical mistakes. One such error is “**Matrix Index is Out of Range for Deletion**” which occurs due to deleting an unspecified number of rows and columns from a given matrix. This error can be fixed by deleting the rows or columns that must lie in the given matrix. This guide has provided examples by generating errors and the solutions to fix them. Understanding them will help you resolve your issues in case such an error occurs in your case.