The error message is clear, the index in position two is invalid because it contains either non-integer values or values less than one.
The index in position two is the ind variable, so look inside that variable and see what the values are. They're going to be integer due to the way you construct ind, but if left is 0 or negative, then some ind values will indeed be invalid.
You can then work backward throught the code to find why left is less than 1. Without any of your inputs it's not something we can answer. Possible reason at first glance is that size(spec, 2) is greater than numHops.
By the way, isn't spec a vector? In which case, numel(spec) would be simpler and safer than size(spec, 2).