Hello!(adsbygoogle = window.adsbygoogle || []).push({});

I consider myself typically a confident Matlab user. But I have encountered a new problem that I have no idea how to solve, so I'm hoping I can find some help.

I have set up a for loop in my code to the effect of

for i = 1:n

if MyArray(i,1) <some value

perform other calculations

end

end

The code runs fine for small values of n. But, when I have a very large n (somewhere near 50,000), I get an error message:

??? Attempted to access MyArray(NaN,1); index must be a positive integer or logical.

And, checking on the value of the index i, it has become NaN.

I don't understand how this is possible as Matlab should be incrementing i itself within the for loop. (I promise I'm not messing with the index inside the for loop!) I thought it might be a problem with the variable type of i, since I think values stored as integers have a relatively small limit, but it looked like i was stored as type long, which should be able to hold much larger than 50,000.

Does anyone have any suggestions? Your help is much appreciated!

-S

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Matlab help: indexing in for loops

**Physics Forums | Science Articles, Homework Help, Discussion**