Matlab Function Behaves Unexpectedly On Array Inputs