我遇到一个问题,我收到错误,我的下标索引是错误的,但它们不是,我有很多检查,以确保它们不是。这段代码几乎每次都有效,所以我不知道为什么这次失败了。
if (idx - ofs <= 0) || (idx - ofs + slen > length(A)) %skip this iteration if it's too close to the beginning or end
if DEBUGMODE
fprintf('Skipping pulse peak at t = %d\n', pulse_peaks(k));
% keyboard;
end
continue;
end
nonSkipped_iterations = nonSkipped_iterations + 1;
% fprintf('placing the %dth peak - idx=%d\n', k, idx);
s = A( idx + (1:slen) - ofs); %Iterate through A, assign all the values between (idx - ofs + 1) and (idx + slen - ofs) to a new vector
它在s = A( idx + (1:slen) - ofs );
上崩溃,在使用dbstop if error
进行测试时,我发现idx+(1:slen)-ofs
是一个有效的索引数组,并且完全在A的长度范围内。< / p>
还有另一个原因,matlab会抱怨下标索引,但是它显示了这个错误吗?
编辑 - 完整错误是:
Subscript indices must either be real positive integers or logicals.
Error in analyse_stun_gun (line 266)
s = A( idx + (1:slen) - ofs); %Iterate through A, assign all the values between (idx - ofs + 1) and (idx + slen - ofs) to a new vector
编辑:我以某种方式失去了我正在处理的服务器的权限,我正在努力恢复,但是:
在违法案件中:
idx is ~ 10 000
ofs is ~ 5 000
slen is ~ 1 000
length(A) is 1 000 000
答案 0 :(得分:1)
未指明idx
,ofs
或slen
的来源是什么,但如果其中任何一个是计算值,如果从任何可能涉及非整数的计算中得到它们,它们可能会从整数值略微偏离。由于MATLAB中默认值为double-precision,因此它们可能容易受到非常小的浮点错误的影响(请参阅here进行讨论)。
简而言之,你应该尝试将它们四舍五入,看看是否有帮助:
s = A(round(idx + (1:slen) - ofs));