changing a and b from vectors to scalars, is a bad idea, and will make your code slow and unpredictable. Changing the type in a variable depending on values creates a type instability, and there is no way for the compiler to predict what types of values will be held in those variables.
Global variables should also be avoided, and it is recommended that code should be inside functions.