I wrote a simple code in Octave,but it keeps reporting parse error which I can't find.The code is
X = magic(3)
m = size(X, 1)
p = zeros(m, 1)
theta = [1;2;1]
hypo = 1./(1.+exp(-X*theta));
for i = 1:m
if hypo(i) > 0.5
p(i) = 1;
else
p(i) = 0;
end
and Octave reports
parse error near line 12 of file F:/my document/machine learning/machine-learning-ex2/ex2/new 1.m
syntax error
error: source: error sourcing file 'F:/my document/machine learning/machine-learning-ex2/ex2/new 1.m'
error: parse error
But,there is nothing in line 12.The last line is 11.I don't know where is wrong.
2 Answers
You're missing an end to terminate the if statement. The correct code should be like this:
X = magic(3)
m = size(X, 1)
p = zeros(m, 1)
theta = [1;2;1]
hypo = 1./(1.+exp(-X*theta));
for i = 1:m
if hypo(i) > 0.5
p(i) = 1;
else
p(i) = 0;
end % <-- you forgot this!
end
The if statement end with endif in octave (see: ), and also the for statement with endfor (see: )
So the correct code would be:
X = magic(3)
m = size(X, 1)
p = zeros(m, 1)
theta = [1;2;1]
hypo = 1./(1.+exp(-X*theta));
for i = 1:m
if hypo(i) > 0.5
p(i) = 1;
else
p(i) = 0;
endif
endfor