>>> index exceeds the number of array elements. index must not exceed 1..

In this blog post we are looking to resolve an error that can occur in MATLAB: index exceeds the number of array elements. index must not exceed 1. This is how our article will be organized.

How to cause the error index exceeds the number of array elements. index must not exceed 1. ?

To reproduce the error in MATLAB you can for example run this code

y = zeros(6,1);
dy = zeros(6,1);
K = 10^(8);
d=5*10^(-1);
r=10^(-1);
for i = 1:6
    ii     = 4 * (i-1) + 1;
    dy(ii) = r * y(ii) * (1 - (y(ii) + y(ii+1)) / K) - d * y(ii+2) * y(ii);
end

In this example i’m getting « Index exceeds the number of array elements. Index must not exceed 6. »

Example for the error ndex exceeds the number of array elements. Index must not exceed in matlab
Example for the error ndex exceeds the number of array elements. Index must not exceed in matlab

Meaning of the error

This error is quite classic, it means that you are trying to access an element in an array/matrix whose index exceeds the maximum index. Example you have an array of 6 elements and you are trying to access the 7th. In this case you get this error « Index exceeds the number of array elements. Index must not exceed 6. »
In our example above we can easily check the value of ii which is calculated from i to check if it exceeds i. For that I adapt code by adding a disp of ii :

y = zeros(6,1);
dy = zeros(6,1);
K = 10^(8);
d=5*10^(-1);
r=10^(-1);
for i = 1:6
    ii     = 4 * (i-1) + 1;
    disp(ii)
    dy(ii) = r * y(ii) * (1 - (y(ii) + y(ii+1)) / K) - d * y(ii+2) * y(ii);
end

We are getting this result :

    1

     5

Index exceeds the number of array elements. Index must not exceed 6.

Error in untitled (line 9)
    dy(ii) = r * y(ii) * (1 - (y(ii) + y(ii+1)) / K) - d * y(ii+2) * y(ii);

>> 

In fact look at :

dy(ii) = r * y(ii) * (1 – (y(ii) + y(ii+1)) / K) – d * y(ii+2) * y(ii);

and if you check the workspace you will see that y is an array of 6 elements then you are trying to access to y(7). 7 exceed the size of the max index 6.

index exceeds the number of array elements. index must not exceed 1. check workspace
dy and y are array of 6 elements.

How to fix the error in MATLAB

First of all, I would like to draw attention to the fact that you don’t have to add displays. You can use the matlab debugger via:

dbstop if error

Also in our case we saw that the error was accessing the array/matrix via an index that exceeds its size. In our case, the loop causes it to access row 7 of a table with a size of 6.

To guard against such a problem, one solution is, for example, to test via conditions if we exceed the maximum index and if so it is replaced by the max index. Example :

dbstop if error
y = zeros(6,1);
dy = zeros(6,1);
K = 10^(8);
d=5*10^(-1);
r=10^(-1);
for i = 1:6
    ii     = 4 * (i-1) + 1;
    if ii >= 5
        ii=4
    end
    dy(ii) = r * y(ii) * (1 - (y(ii) + y(ii+1)) / K) - d * y(ii+2) * y(ii);
end
index exceeds the number of array elements. index must not exceed 1.  Matlab dbstop if error
Matlab dbstop if error

Retour en haut