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

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

Please share the size of your input data to further understand the problem. Thanks.
Please do not let the readers guess, in which line the error occurs. Post a copy of the complete error message instead.
Here is the workspace
I think the error is in value_data_1_smoothed_bin = value_data_1_smoothed(bin_data1); this row!
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.
Matlab shows me this, but I don't understand the problem
bin_data1 = 1:(bin*new_fs):size(data_1,1)-1;
Unrecognized function or variable 'bin'.
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.
@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);
This is my script, the problem is the last part:
% to filter and downsample the LFP signal
new_fs = 1000;
new_time = downsample(time,fs/new_fs);
Unrecognized function or variable 'time'.
movingwin = [10 5]; % set the moving
params.Fs = 1000; % sampling frequency
params.fpass = [2 150]; % frequencies of interest
bin=30; % in seconds
params.tapers = [3 5]; % tapers
params.trialave = 0; % average over trials
params.err = 0; % no error computation
data_1 = LFP1_d; % data from monkeys 1
[S1,t,f1] = mtspecgramc(data_1,movingwin,params); % compute spectrogram
subplot(121)
plot_matrix(S1,t,f1);
xlabel('Time');
ylabel('Frequency'); % plot spectrogram
colorbar;
colormap('jet');
xmax = max(213.760);
xmin = min(546.740);
xline([xmax xmin],'--',{'r_M1'})
xmax = max(871.620);
xmin = min(1015.860);
xline([xmax xmin],'--',{'r'})
xmax = max(1048.520);
xmin = min(1170.800);
xline([xmax xmin],'--',{'s'})
data_2 = LFP2_d; % data from monkeys 2
[S2,t,f2] = mtspecgramc(data_2,movingwin,params); % compute spectrogram
subplot(122);
plot_matrix(S2,t,f2);
xlabel('Time'); % plot spectrogram
ylabel('Frequency');
colorbar;
colormap('jet');
xmax = max(213.760);
xmin = min(546.740);
xline([xmax xmin],'--',{'r_M1'})
xmax = max(871.620);
xmin = min(1015.860);
xline([xmax xmin],'--',{'r'})
xmax = max(1048.520);
xmin = min(1170.800);
xline([xmax xmin],'--',{'s'})
R_rest_1 = corrcoef(data_1(817.620:1015.860),data_2(817.620:1015.860));
R_rest_2 = corrcoef(data_1(1627.862:1751.560),data_2(1627.862:1751.560));
R_rest_3 = corrcoef(data_1(2413.580:2549.120),data_2(2413.580:2549.120));
R_stimulation_with_strobe_1 = corrcoef(data_1(1048.520:1170.800),data_2(1048.520:1170.800));
R_stimulation_with_strobe_2 = corrcoef(data_1(1786.080:1908.360),data_2(1786.080:1908.360));
R_stimulation_with_strobe_3 = corrcoef(data_1(2590.520:2703.260),data_2(2590.520:2703.260));
movingwin = [10 5]; % set the moving
params.Fs = 1000; % sampling frequency
params.fpass = [2 150]; % frequencies of interest
params.tapers = [3 5]; % tapers
params.trialave = 0; % average over trials
params.err = 0; % no error computation
[S3,f3] = mtspectrumc(data_1,params);
subplot(121)
plot_vector(S3,f3);
xlabel('Frequency'); % plot spectrum
ylabel('Spectrum dB');
[S4,f4] = mtspectrumc(data_2,params);
subplot(122)
plot_vector(S4,f4);
xlabel('Frequency'); % plot spectrum
ylabel('Spectrum dB');
%%% Plot Power-Time of spectrum %%%
bin_data1 = 1:(bin*new_fs):size(data_1,1)-1;
data_1_smoothed = smoothdata(data_1,'gaussian',5);
value_1 = data_1_smoothed(bin_data1);
time_1 = (1:bin:(size(data_1)/new_fs))-1;
plot(time_1,value_1,'r');
title('LFP-Time'); xlabel('Time (s)'); ylabel('LFP');
hold on
bin_data2 = 1:(bin*new_fs):size(data_2,1)-1;
data_2_smoothed = smoothdata(data_2,'gaussian',5);
value_2 = data_2_smoothed(bin_data2);
time_2 = (1:bin:(size(data_2)/new_fs))-1;
plot(time_2,value_2,'b');
title('LFP-Time'); xlabel('Time (s)'); ylabel('LFP');
bin_data1 = 1:(bin*new_fs):size(data_1,1)-1;
time_1 = (1:bin:(size(data_1,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(f_1>=2 & f_1<=150);
value_data_1_smoothed = smoothdata(value_data_1,'gaussian',5);
value_data1_smoothed = value_data_1_smoothed(bin_data1);
plot(time_1,value_data1_smoothed,'r');
title('2-150 Hz'); xlable('Time (s)'); ylabel('Power (dB)');
The problem is in the last part because I cannot to binning the value_data_1_smoothed because "Index must not exceed 4850. " The value data_1_smoothed is a vector 4850x1, while the bin_data1 are a vector 1x91.
Thank you so much for your help!
No, the problem is near the beginning. Look above. See where it says
Unrecognized function or variable 'time'.
You must define the time variable.
No, I definided time but I didnt write here
Is it not possible for you to include an executable code that only errors at the position where you encounter the difficulties ?
In order to debug this further, can you please help with the time variable used in second line?
@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;
?
time_1 = (1:bin:(size(data_1)/new_fs))-1; this is the second line
The problem is the index of value_data1_smoothed = value_data_1_smoothed(bin_data1);
@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.
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.
I couldn't load the file because it is exceeds the dimension. I tried also in zip file, but I cannot. Do you have any idea?
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.
I know what is the problem, my bin_data1 is too big than my value_data_1 and it is no possible the binning, but I need this binning fot the time. How can I resolve this?
@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.
@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.
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)

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.

Asked:

on 5 Nov 2022

Edited:

on 12 Nov 2022

Community Treasure Hunt

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

Start Hunting!