在八度中找到解析错误



我在八度中写了一个简单的代码,但是它一直在报告解析错误,我找不到。

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

和八度报告

 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行中没有什么。最后一行是11.我不知道哪里错了。

您缺少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

if语句以 endif结尾(请参阅:https://www.gnu.org/software/octave/octave/octave/doc/interpreter/the-if-statement.html(,另外带有endforfor语句(请参阅:https://www.gnu.org/software/octave/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

相关内容

  • 没有找到相关文章

最新更新