在 Octave 中查找解析错误

Find a parse error in Octave

我用 Octave 写了一个简单的代码,但它一直报告解析错误,我不能 find.The 代码是

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

和 Octave 报告

 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

但是,12.The行什么都没有,最后一行是11.I不知道哪里错了。

if 语句以八度 endif 结尾(参见: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

所以正确的代码是:

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

您缺少 end 来终止 if 语句。正确的代码应该是这样的:

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