为什么在for循环中发生超出范围的错误?

时间:2019-01-10 14:59:00

标签: octave indexoutofboundsexception

发生超出范围的错误。

这是八度语言。

for ii=1:1:10
    m(ii)=ii*8
    q=m(ii)
    if (ii>=2)
        q(ii).xdot=(q(ii).x-q(ii-1).x)/Ts;
    end
end

但是错误提示

 q(2): out of bound 1

我该如何解决?

1 个答案:

答案 0 :(得分:0)

对于这种类型的分配,您不需要循环,而且无论如何都需要定义Ts。 要计算差异增加,您可以使用diff

x=(1:1:10)*8
x =

    8   16   24   32   40   48   56   64   72   80

octave:5> Ts=2
Ts =  2
octave:6> xdot=diff(x)/Ts
xdot =

   4   4   4   4   4   4   4   4   4

octave:7> size(x)
ans =

    1   10

octave:8> size(xdot)
ans =

   1   9
相关问题