title: Implementing "Gradient Descent Algorithm" in Matlab
body: <p>I'm solving a programming assignment in <code>Machine Learning</code> course. In which I've to implement <code>Gradient Descent Algorithm</code> like below</p>
  
  <p><img src="https://i.stack.imgur.com/V8bcE.png" alt="enter image description here"></p>
  
  <p>I'm using the following code in Matlab</p>
  
  <pre><code>data = load('ex1data1.txt');
  % text file conatins 2 values in each row separated by commas
  X = [ones(m, 1), data(:,1)];
  theta = zeros(2, 1);
  iterations = 1500;
  alpha = 0.01;
  
  function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
  m = length(y); % number of training examples
  J_history = zeros(num_iters, 1);
  for iter = 1:num_iters
     k=1:m;
     j1=(1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k))
     j2=((1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k)))*(X(k,2))
     theta(1)=theta(1)-alpha*(j1);
     theta(2)=theta(2)-alpha*(j2);
     J_history(iter) = computeCost(X, y, theta);
  end
  end
  
  theta = gradientDescent(X, y, theta, alpha, iterations);
  </code></pre>
  
  <p>On running the above code I'm getting this error message</p>
  
  <p><img src="https://i.stack.imgur.com/RLjfR.png" alt="enter image description here"></p>
  
  <p>Its clear from the error message that, the result of the below expression</p>
  
  <pre><code>((1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k)))*(X(k,2))
  </code></pre>
  
  <p>Is a vector, which we are trying to save in a scalar variable j2. I think X(k,2) is creating a problem, I've used it as an index to vector X to fetch a value from kth row and 2nd column. But On the other hand the whole vector is getting multiplied, Please suggest me how can I fix it.</p>