From 74d2383b6b17e6bf805cd23928448d99efc60684 Mon Sep 17 00:00:00 2001 From: Dhananjay Nene Date: Sat, 22 Oct 2011 16:45:37 +0530 Subject: [PATCH] Replacing a loop by matrix operation. Re:i was thinking in do it in that way (like clojure repeat) in gradient descend but maybe it is not possible --- exercises/mlclass-ex1/gradientDescent.m | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) 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