首页 > 解决方案 > for循环中的索引问题?位置 2 中的索引超出数组边界(不得超过 27630)。MATLAB

问题描述

我有一个代码来处理我的声学数据(高度计探测来监测浅水中的海草高度)。数据来自每个调查部分的单独文件,因此我总共有 6 个不同大小的 .txt 文档。我已经加载了所有内容,但是当我尝试每 7 次 ping 平均循环一次时,我得到了错误。

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.

这就是代码部分的样子,非常感谢任何帮助。我还将复制可变尺寸。提前感谢您的帮助。此外,我已经在之前的两个数据文件上运行了完整的脚本,它工作得非常好,所以我不确定在尝试运行第三个时出了什么问题。再次感谢。

我正在尝试索引两列(时间和 pings),但我不确定如何以最好的方式做到这一点:// 下面是我的一半代码和我一直在使用的循环,开始时的位(for循环和 k_ind) 当我需要 27630 时创建一个大小为 28022 的矩阵,但我不确定如何解决这个问题。

% index the VA500 pings
k = regexp(C{2},'sig');
k_ind = [];
for i = 1:length(k)
    if cell2mat(k(i)) == 1
        k_ind = [k_ind; i];
    end
end
ping_id = k_ind+1;

% find time of each ping
ping_time = time(ping_id);

% Find number of rows in ping, and create record of depth and correlation
% value (proxy for acoustic intensity)
data = char(C{2});
ping_corr = [];
ping_depth = [];
for i = 1:length(ping_id)-1
    tmp = ping_id(i+1)-ping_id(i);
    try
        for j = 1:tmp
            ping_depth(j,i) = str2num(data(ping_id(i)+j,1:5)); % depth
            ping_corr(j,i) = str2num(data(ping_id(i)+j,7:end)); % correlation value (acoustic intensity)
        end
    end
end
  
ping_num = 1:1:length(ping_corr);
%% 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双倍,bin_corr 161x28022 双倍。

我不知道如何解决这个问题并为与 ping 大小相同的数组预分配,因为由于某种原因它的大小不同。提前感谢您的帮助,非常感谢。

标签: matlabloopsfor-loopindexingmatlab-figure

解决方案


推荐阅读