Insufficient info to tell -- the problem would appear to be in what isn't shown.
Use debugger to stop on the error and examine the specific case values. Set
The above code loop could be written as
If I<size(G1,1) then substitute the vector 1:I for I in the first subscript expression.