This is a floating point issue. You can see this by typing
format long
in the MATLAB Command Window and displaying all of the values you compute in each step. The numbers period and dt look exact but are not, and their division is not exactly 300. This is because some real values are not represented as floating point numbers, so computers round those computations to the nearest floating point number. You can use the round function to ensure that the input to ones is an integer.