diff --git a/exercises/mlclass-ex1/gradientDescent.m b/exercises/mlclass-ex1/gradientDescent.m index 2fde574..61ed7ab 100644 --- a/exercises/mlclass-ex1/gradientDescent.m +++ b/exercises/mlclass-ex1/gradientDescent.m @@ -6,7 +6,6 @@ % Initialize some useful values m = length(y); % number of training examples J_history = zeros(num_iters, 1); -num_vars=size(X,2); for iter = 1:num_iters @@ -17,11 +16,7 @@ % Hint: While debugging, it can be useful to print out the values % of the cost function (computeCost) and gradient here. % - theta_tmp=zeros(num_vars,1); - for j=1:num_vars - theta_tmp(j)= theta(j) - (alpha/m) * sum(((X*theta)-y).*X(:,j)); - end - theta=theta_tmp; + theta = theta - (X' * (X * theta - y) * alpha / m); % ============================================================ % Save the cost J in every iteration