You can use vectorized operations in Octave/Matlab. You should not simply iterate over entire vector, if your program language let you vectorize operations. R, Octave, Matlab, Python (numpy) allow this operation. For example, you can get scalar production, if theta = (t0, t1, t2, t3) and X = (x0, x1, x2, x3) in the next way: theta * X' = (t0, t1, t2, t3) * (x0, x1, x2, x3)' = t0*x0 + t1*x1 + t2*x2 + t3*x3. Result would be scalar.
For example, you can vectorize h in your code:
H = (theta'*X')';
S = sum((H - y) .^ 2);
J = S / (2*m);
Hope this answer helps.