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

20 views (last 30 days)
Hi everyone,
this is my code:
bin_data1 = 1:(bin*new_fs):size(data_1,1)-1;
time_1 = (1:bin:(size(data_1)/new_fs))-1;
step = data_1(bin_data1:bin_data1+(bin*new_fs)-1);
[S_1,f_1] = mtspectrumc(step,params); % compute spectrum
value_data_1 = S_1(find((f_1>=2 & f_1<=150)==1));
value_data_1_smoothed_bin = value_data_1_smoothed(bin_data1);
title('2-150 Hz'); xlable('Time (s)'); ylabel('Power (dB)');
When I do running the script, it appears a problem:
Index exceeds the number of array elements. Index must not exceed 620757.
But what is the index?
Torsten on 12 Nov 2022
Edited: Torsten on 12 Nov 2022
Thought I could do it unnoticed :-)
But although I'm from Germany (like you ?) where the comma in front of "that" is obligatory, it took some time before I understood what you meant in your comment. So I took the risk to edit away the comma.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 8 Nov 2022
What is element number 11 of the following vector?
x = 1:10
The answer to the question is "it doesn't exist." In situations where MATLAB asks that kind of question, the way it says "it doesn't exist" is to throw the error you're receiving.
To determine the root cause of this failure, set a breakpoint on the line of code where you receive this error then run your code. [If this code is in a loop and this error occurs only after a few iterations, set an error breakpoint as described in the Error Breakpoints section on that documentation page instead.] When MATLAB reaches the breakpoint, look at the size of the variable into which you're indexing and the max of the arrays you're using as indices.
Once you've confirmed that you're in this "asking for the 11th element of a 10 element vector" scenario, look backwards in your code to see where you're potentially changing the size or values of either of the arrays involved in that indexing operation. This could involve setting breakpoints on earlier lines of your code and stepping through line-by-line looking at those arrays in the Workspace Browser to detect when those arrays change.


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!