In many places in MATLAB, the values 0 and false can be used interchangeably.
Indexing is one of the exceptions where they are treated differently.
x([false true true]) = [42, -999]
x([0 1 1]) = [42, -999]
Array indices must be positive integers or logical values.
You could make G a logical array by calling logical on it or allocating it as a false array before assigning values into its elements. In that case the size of the array you're assigning into Q would need to be based on the number of true values in the index vector rather than the number of elements. In the example above, the logical index vector [false true true] had 3 elements but only 2 of them were true, so I could only assign 2 elements to those locations.