Farhan
Farhan

Reputation: 5

Summation using for loop in MATLAB

J = 0;
sumTerm = 0;

for i=1:m

sumTerm = sumTerm + ((theta(1)+theta(2)*X(i))-y(i)).^2;

end

J = (1/2*m)*sumTerm;

Is this the right way to do summation ?

Upvotes: 0

Views: 469

Answers (2)

rayryeng
rayryeng

Reputation: 104565

That's correct, but you'll want to implement that vectorized instead of using loops. You can take advantage of this by using linear algebra to compute the sum for you. You can compute theta(1) + theta(2)*X(i) - y(i) for each term by first creating the matrix X that is a matrix of points where the first column is appended with all ones and the next column contains your single feature / data points. You would finally compute the difference between the output from the prediction line and the true output for each data point by X*theta - y which would thus produce a vector of differences for each data point. This is also assuming that your array of points and theta are both column vectors, and I believe that this is the right structure since this looks like you're implementing the cost function for univariate linear regression from Andrew Ng's Machine Learning course.

You can then compute the dot product of this vector with itself to compute the sum of square differences, then you can divide by 2*m when you're done:

vec = [ones(m,1) X]*theta - y;
J = (vec.'*vec) / (2*m); %'

The reason why you should pursue a linear algebra solution instead is because native matrix and vector operations in MATLAB are very, very fast and if you can find a solution to your computational problems with linear algebra, it'll be the fastest you can ever get your code to compute things.

For example, see this post on why matrix multiplication in MATLAB is amongst the fastest when benchmarking with other platforms: Why is MATLAB so fast in matrix multiplication?

Upvotes: 1

NKN
NKN

Reputation: 6434

How about this:

J = 0.5 * sum(((theta(1)*ones(size(X))+theta(2)*X)-y).^2)/m

Or as @rayryeng pointed out, you can even drop the ones

J = 0.5 * sum(((theta(1)+theta(2)*X)-y).^2)/m

Upvotes: 2

Related Questions