i was trying to implement Gradient Descent (linear regression with one variable).I have some doubts with the below code. 

In the attachment ex1data1 column 1 and 2 represent X and y respectively.
Initially i was trying to implement the algorithm as below

```
for iter=i:iterations

 for i=1:2
     h=(theta'*X')';
     s=sum((h-y).*X(i));
     s1=s*alpha/m;
     theta(i)=theta(i)-s1;

  end
 ..
end

```

but i could not arrive at the optimal value for theta.

Then i tried vectorization 

```
for iter=i:iterations
   h=(theta'*X')';
   theta = theta -((1/m) * (h - y)' * X)' * alpha;
end

```

and got the optimal value.

Could you please explain what is the difference between both code? i found it same. its been told that vectorization is preferred over loop, but just want to know out of curiosity. Is there anything particular i need to concentrate while trying to find a vectored formula?