Consider that you are doing vector operation, change your cost function to the following:
(1 / m) * sum(((-y) .* (log(h)) - ((1 - y) .* log((1-h)))));
and your gradient to the following:
grad = (1./m) * (x' * (h - y))
Although the latter is just for precedence reassuring.