You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Index exceeds the number of array elements. Index must not exceed 620757.
3 views (last 30 days)
Show older comments
Felicia DE CAPUA
on 5 Nov 2022
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
Here is the workspace
I think the error is in value_data_1_smoothed_bin = value_data_1_smoothed(bin_data1); this row!
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.
Image Analyst
on 5 Nov 2022
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.
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
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!
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.
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;
?
Felicia DE CAPUA
on 8 Nov 2022
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);
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 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?
Jan
on 8 Nov 2022
Edited: Jan
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 11 Nov 2022
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?
Jan
on 11 Nov 2022
Edited: Torsten
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.
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)