Melinda W
Melinda W

Reputation: 75

Find a parse error in Octave

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.

Upvotes: 2

Views: 36652

Answers (2)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

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

Upvotes: 3

user000001
user000001

Reputation: 33327

The if statement end with endif in octave (see: https://www.gnu.org/software/octave/doc/interpreter/The-if-Statement.html), and also the for statement with endfor (see: https://www.gnu.org/software/octave/doc/interpreter/The-for-Statement.html)

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

Upvotes: 1

Related Questions