0

I have an issue where I am getting an error that my subscript indices are wrong, but they are not, and I have many checks to ensure they are not. This code works almost every time, so I don't know why it failed this time.

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

It crashes on s = A( idx + (1:slen) - ofs );, which when testing using dbstop if error, I see that idx+(1:slen)-ofs is a valid array of indices, and is well within the bounds of the length of A.

Is there another reason matlab would complain about subscript indices, but it's showing this error instead?

Edit - the full error is :

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

Edit : I've somehow lost permission on the server I was working on, I'm working on getting that back, but:

In the offending case :

idx is ~ 10 000
ofs is ~ 5 000
slen is ~ 1 000
length(A) is 1 000 000
Brydon Gibson
  • 1,179
  • 3
  • 11
  • 22
  • May you please paste the full message of error? – Fra93 Jul 12 '17 at 14:42
  • Please give an example of your `idx+(1:slen)-ofs` and the dimensions of `A`. Even better would be if you could provide enough code for us to test this issue ourselves. – Wolfie Jul 12 '17 at 14:52
  • as stated I've lost server access, but when I get it back I'll post a minimal working example – Brydon Gibson Jul 12 '17 at 15:00

1 Answers1

1

It's not specified what the source of idx, ofs, or slen is, but if any of them are computed values that were gotten from any sort of calculation that may involve non-integers, they could be slightly off from an integer value. Since all values are double-precision by default in MATLAB, they could be susceptible to very small floating-point errors (see here for a discussion).

In short, you should try rounding them off and see if that helps:

s = A(round(idx + (1:slen) - ofs));
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • This is something I thought about, and I visually confirmed that they are not decimals, but I'll give round a shot – Brydon Gibson Jul 12 '17 at 15:12
  • @BrydonGibson: Visual confirmation may not always be good enough, as the linked question shows (i.e. the way numbers are displayed can sometimes be misleading). – gnovice Jul 12 '17 at 15:15
  • I missed the link on mobile - thanks, that makes sense and I'll try it out. – Brydon Gibson Jul 12 '17 at 15:21