Index exceeds the number of array elements. Index must not exceed 620757.
Show older comments
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=smoothdata(value_data_1,'gaussian',30);
value_data_1_smoothed_bin = value_data_1_smoothed(bin_data1);
plot(time_1,value_data_1_smoothed_bin,'r');
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?
24 Comments
Maik
on 5 Nov 2022
Please share the size of your input data to further understand the problem. Thanks.
Jan
on 5 Nov 2022
Please do not let the readers guess, in which line the error occurs. Post a copy of the complete error message instead.
Felicia DE CAPUA
on 5 Nov 2022
Torsten
on 5 Nov 2022
Then you should check
bin_data1(end)
and
numel(value_data_1_smoothed)
If
numel(value_data_1_smoothed) < bin_data1(end)
, MATLAB will throw an error.
Felicia DE CAPUA
on 5 Nov 2022
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=smoothdata(value_data_1,'gaussian',30);
value_data_1_smoothed_bin = value_data_1_smoothed(bin_data1);
plot(time_1,value_data_1_smoothed_bin,'r');
title('2-150 Hz'); xlable('Time (s)'); ylabel('Power (dB)');
I know Jan already explicitly and directly asked you for the data, but I don't know why you refused. Make it EASY for people to help you, not hard. Do we have bin? No. You would not attach it for some reason. Why not? Why not attach your full script or at least the variables we need to run it, like bin, new_fs, params, etc. in a .mat file with the paperclip icon.
Jan
on 5 Nov 2022
@Felicia DE CAPUA: Please post text output as text, not as screenshot. This makes the reading easier, especially on smartphones. You do not have to think where the error occurs, but you should find this explicitly mentioned in the error message.
This line is fragile:
time_1 = (1:bin:(size(data_1)/new_fs))-1;
size() replies a vector. The colon operator a:b:c uses the first element only, if its operands are vectors. But prefer to write explicitly, which dimension is wanted, perhaps:
time_1 = (1:bin:(size(data_1, 1)/new_fs))-1;
% ^
In th line:
value_data_1 = S_1(find((f_1>=2 & f_1<=150)==1));
you can omit the find() and the comparison with 1. This is called "logical indexing" and faster than using the indices:
value_data_1 = S_1(f_1>=2 & f_1<=150);
Felicia DE CAPUA
on 6 Nov 2022
Edited: Image Analyst
on 6 Nov 2022
Image Analyst
on 6 Nov 2022
No, the problem is near the beginning. Look above. See where it says
Unrecognized function or variable 'time'.
You must define the time variable.
Felicia DE CAPUA
on 6 Nov 2022
Torsten
on 6 Nov 2022
Is it not possible for you to include an executable code that only errors at the position where you encounter the difficulties ?
Suvansh Arora
on 8 Nov 2022
In order to debug this further, can you please help with the time variable used in second line?
Jan
on 8 Nov 2022
@Suvansh Arora: Which one is the "second line"?
new_time = downsample(time,fs/new_fs);
or
time_1 = (1:bin:(size(data_1)/new_fs))-1;
?
Suvansh Arora
on 8 Nov 2022
new_time = downsample(time,fs/new_fs);
Felicia DE CAPUA
on 8 Nov 2022
Jan
on 8 Nov 2022
@Felicia DE CAPUA: The discussion looks like the state of confusion is perfect.
By the way,
time_1 = (1:bin:(size(data_1)/new_fs))-1;
can be written as:
time_1 = 0:bin:(size(data_1) / new_fs - 1);
The indexing problem means, that the array
value_data_1 = S_1(f_1>=2 & f_1<=150);
is shorter than
size(data_1,1)-1
Due to the lack of comments, it is impossible to guss securely, what the purpose of the code is and in consequence I cannot guess, why you assume, that bin_data1 is an adequate index of the array value_data_1_smoothed.
Suvansh Arora
on 8 Nov 2022
Please comment out the code that is creating issues for you and upload the rest of the ".m" file along with the "MATLAB Data" file containing all the variables used.
Felicia DE CAPUA
on 8 Nov 2022
I've seen this line:
R_rest_1 = corrcoef(data_1(817.620:1015.860),data_2(817.620:1015.860));
This looks rather strange. Indices must have integer values. So this line should not run at all.
These lines are strange also:
xmax = max(213.760); % ???
xmin = min(546.740); % ??? Why?
Personally, I've lost the overview completely:
- "I couldn't load the file because it is exceeds the dimension." - Which file do you want to load from where? Which dimensions are exceeded?
- "I tried also in zip file" - what did you try?
I suggest to restart from scratch. Re-write the code. Create the values at first and move the visualization at the end. Then post some inputs, the relevant part of the code (omit the visualisation and the code behind the first error, becauce it is nor relevant to solve the problem) and a copy of the complete error message. Add meaningful comments in the code:
[S_1,f_1] = mtspectrumc(step,params); % compute spectrum
"compute spectrum" is not useful, because it does not add new information. That "mtspectrumc" computes a spectrum is more or less obvious. But what is the purpose of this line:
value_data_1 = S_1(f_1>=2 & f_1<=150);
How large is value_data_1 afterwards and what does it represent?
By the way, "value_data_1" is a bad choise for the name of a variable. The names "value_data_1_smoothed" and "value_data1_smoothed" cry for confusion also.
Focus on the failing part and post it in a way, which let the readers reproduce the error.
Felicia DE CAPUA
on 9 Nov 2022
Felicia DE CAPUA
on 11 Nov 2022
@Felicia DE CAPUA: Let me summarize: You ask for a method to perform the binning inspite of the fact that it is not possible.
This is not the way programming works. Take a break. Drink a cup of coffee. Rethink, what you exactly want to do. Omit all tries to solve unsolvable problems but concentrate on solution, which can be found.
Jan
on 11 Nov 2022
@Torsten: Thanks for removing the misplaced comma from my comment.
As non-native speaker I'm aware that my spelling has a potential for improvements. I takes some time to check, what has been modified in an edited comment. Therefore I appreciate fixing typos, if it really improves the comprehensibility of my contributions.
Answers (1)
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.
Categories
Find more on Matrix Indexing 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!