Categories

# Matlab Index Exceeds Matrix Dimensions Error

In MATLAB, ‘index exceeds matrix dimensions error’ is encountered when we try accessing an element in the array/matrix which is not in index or not present in the array/matrix. The simple way to resolve this error is by ensuring that we are trying to access the element which is present in the matrix or array. The length of the array should be greater than or equal to the index that we are calling or trying to access.

In the latest versions of MATLAB (Starting from R2018b), the error message is:

“Index exceed number of array elements”

## Syntax:

If A is an array, then we access any element of it using the below syntax:

``E = A(n)``

where ‘n’ is the index of the element that we want to access.

If ‘n’ is greater than the number of elements in the array, then we get the ‘index exceeds matrix dimensions’ error in case of a matrix or ‘index exceeds the number of array elements’ error in case of an array.

## Examples of Matlab Index Exceeds Matrix Dimensions

Let us now understand how we get this error in MATLAB and how to resolve it.

## Example 1:

In the first example, we will create an array with 5 elements and will try accessing the 7th element of this array (which does not exist). This will result in an index exceed dimensions error. The steps to be followed for this example are:

1. Initialize the array
2. Try accessing the element which is out of index

## Code:

``A = [3, 5, 1, 7, 10]``

[Creating the array with 5 elements]

``E = A(7)``

[Code to access the 7th element of the array A]

[Since the 7th element does not exist in array A, we will get the error message]

This is how our input and output will look like in Matlab command window:

## Input:

``A = [3, 5, 1, 7, 10] E = A(7)``

## Output:

As we can see in the Output, since we are trying to access an element that does not exist in the array, we got the error message. Now, to overcome this error, we must try accessing an element that is in the range, i.e for E = A(n), ‘n’ must be less than or equal to 5.

## Example 2:

In this example, we will create a matrix of size 3 X 3 and will try accessing an element in the 4th row (which does not exist). This will result in index exceed dimensions error. The steps to be followed for this example are:

1. Initialize the matrix
2. Try accessing the element which is out of index

## Code:

``A = [3, 5, 1; 7, 10, 5; 3, 6, 9]``

[Creating the matrix of the order 3 X 3]

``E = A(4, 2)``

[Code to access the 2nd element in the 4th row of the matrix A]

[Since the 4th row does not exist in the matrix A, we will get the error message]

This is how our input and output will look like in Matlab command window:

## Input:

``A = [3, 5, 1; 7, 10, 5; 3, 6, 9] E = A(4, 2)``

## Output:

As we can see in the Output, since we are trying to access an element that does not exist in the matrix, we got the error message. Now, to overcome this error, we must try accessing an element that is in the range, i.e for E = A(m, n), ‘m’ and ‘n’ must be less than or equal to 3.

## Example 3:

In this example, we will create a matrix of size 4 X 4 and will try accessing an element in the 5th column (which does not exist). This will result in an index exceed dimensions error. The steps to be followed for this example are:

1. Initialize the matrix
2. Try accessing the element which is out of index

## Code:

``A = [3, 15, 10, 4; 4, 5, 8, 0; 13, 5, 7, 10; 3, 4, 1, 6]``

[Creating the matrix of the order 4 X 4]

``E = A(4, 5)``

[Code to access the 4th element in the 5th column of the matrix A]

[Since the 5th column does not exist in the matrix M, we will get the error message]

This is how our input and output will look like in Matlab command window:

## Input:

``````A = [3, 15, 10, 4; 4, 5, 8, 0; 13, 5, 7, 10; 3, 4, 1, 6]
E = A(4, 5)``````

## Output:

As we can see in the Output, since we are trying to access an element that does not exist in the matrix, we got the error message. Now, to overcome this error, we must try accessing an element that is in the range, i.e for E = A(m, n), ‘m’ and ‘n’ must be less than or equal to 4.