Can Matrices be passed as arguments in Octave?

1.5k views Asked by At

I am implementing the non-vectorized form of the cost function in octave. This is the code from my .m file

function computeCost(X, y, theta)
sigma=0;
theta0 = 0;
m = length(y);

for i = 1:m
sigma = sigma+ theta*X(i)-y(i);
end;

J = ((sigma)^2)/2*m;

end;

My octave code is:

>> X= [1,1; 1,2; 1,3; 1,4;];
>> y= [2;4;6;8];
>> J = computeCost(X, y, 0.5);

where X and y are matrices. However, I am getting this output on my CLI Window:

Error: computeCost(X, y, 0.5) undefined near line 1, column 5

I've checked my code, there is no apparent issue. Is it because Octave does not accept matrices as parameters for its functions?

1

There are 1 answers

0
Georg W. On BEST ANSWER

The answer to your question is clearly YES: The name MATLAB is an abbreviation of Matrix laboratory. Octave and Matlab are specially designed to facilitate working with matrices.

The problem in your code is: Your function definition is incomplete. You have not defined J as return value. The error message you see is a bit missleading because it should state column 10 as place of the error. When you change the first line of your code to

function J = computeCost(X, y, theta)

It will work as expected and output the value 648.