Index in position 2 exceeds array bounds (must not exceed 27630) of a for loop?

1 view (last 30 days)
Daryna Butash
Daryna Butash on 8 Jul 2021
Edited: Cris LaPierre on 13 Jul 2021
Hi, I have a code to process my acoustic data (altimeter soundings to monitor seagrass heights in shallow water). The data comes in separate files for each survey section and so I have a total of 6 different sized .txt docs. I have loaded everything in but when I try to average in a loop every 7 pings I am getting and error. Index in position 2 exceeds array bounds (must not exceed 27630).
This is what the section of code looks like, any help is much appreciated. I will also copy the variable sizes in. Thank you for help in advance.
Also I have already ran the full script on two previous data files and it worked absolutely fine so I am unsure what's gone wrong when trying to run the the third. Any help to get the loop working again would be much appreciated, thank you in advance again. I will also attach the full code and txt file for better overview of my problem. Thanks again.
%% Create ping average dataset (moving window).
% Make each ping record an average signal from the surrounding x number of pings (if x is 2 then average is 5 ping moving window).
x = 3;
bin_depth = 0:0.05:8; % depths bins in which to average (change the middle value to change resolution (set currently at 0.05m with max depth 10m)
bin_corr = zeros(length(bin_depth),length(ping_id-(x*2))); % create blank matrix
count = 1;
for i = x+1:length(bin_corr(1,:))-(x+2)
tmp_depth = ping_depth(:,i-x:i+x); % find all depth values for x pings before and after ping i
tmp_depth = tmp_depth(:);
tmp_corr = ping_corr(:,i-x:i+x); % find all depth values for x pings before and after ping i
tmp_corr = tmp_corr(:);
tmp_bins = discretize(tmp_depth,bin_depth); % allocate data to depth bins
for ii = 1:length(bin_depth)
bin_corr(ii, count) = nanmean(tmp_corr(tmp_bins == ii)); % calc bin average values for each ping window
if isnan(bin_corr(ii, count)) == 1
bin_corr(ii, count) = 0;
end
end
count = count+1;
end
k_ind 28022x1 double,
ping_corr 48x27630 double,
ping_depth 48x27630 double,
ping_id 28022x1 double,
k 390908x1 cell,
ping_num 1x27630 double,
ping_time 28022x1 double,
time 390908x1 double ,
tmp_bins 336x1 double,
tmp_corr 336x1 double,
tmp_depth 336x1 double,
data 390908x37 char,
bin_depth 1x161 double,
bin_corr 161x28022 double.
  2 Comments
Daryna Butash
Daryna Butash on 8 Jul 2021
Hi, the only error message I get is this ‘ Index in position 2 exceeds array bounds (must not exceed 27630).’ and that’s the full red text.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 8 Jul 2021
Edited: Cris LaPierre on 13 Jul 2021
Here is the full error message I get. It's helpful to know what line is causing the error.
Index in position 2 exceeds array bounds (must not exceed 27630).
Error in VA500readv2 (line 55)
tmp_depth = ping_depth(:,i-x:i+x); % find all depth values for x pings before and after ping i
The size of ping_depth is 48x27630. You are indexing in position 2 (columns) using i-x:i+x. At some point, this exceeds the number of columns of ping_depth (27630).
You likely need to modify how you set the number of times your for loop runs so that i+x does not exceed 27630. You currently use length(bin_corr(1,:))-(x+2), and bin_corr(1,:) has a length of 28022.
As a simple example, consider the following code:
A = [1 2 3; 4 5 6];
% This works
A(2,2)
ans = 5
% This does not because # columns = 3, so column index cannot exceed 3
A(2,20)
Index in position 2 exceeds array bounds (must not exceed 3).

Community Treasure Hunt

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

Start Hunting!

Translated by