-3

我在看一些 Matlab 代码(没有软件)。我想知道以下内容的作用:

d=[0 diff(fM)]

其中 fM 本身就是一个数组。

4

1 回答 1

0

Octave 中的帮助文档diff(我认为 Matlab 等效的工作方式与任何其他 Matlab/octave 函数相同)

help diff
 `diff' is a function from the file /usr/share/octave/3.2.4/m/general/diff.m

 Function File:  diff (X, K, DIM)
 If X is a vector of length N, `diff (X)' is the vector of first
 differences  X(2) - X(1), ..., X(n) - X(n-1).

 If X is a matrix, `diff (X)' is the matrix of column differences
 along the first non-singleton dimension.

 The second argument is optional.  If supplied, `diff (X, K)',
 where K is a non-negative integer, returns the K-th differences.
 It is possible that K is larger than then first non-singleton
 dimension of the matrix.  In this case, `diff' continues to take
 the differences along the next non-singleton dimension.

 The dimension along which to take the difference can be explicitly
 stated with the optional variable DIM.  In this case the K-th
 order differences are calculated along this dimension.  In the
 case where K exceeds `size (X, DIM)' then an empty matrix is
 returned.

所以基本上,diff返回一个带有n - 1条目的向量,发布的代码是创建一个新数组,在开头添加一个零,因此新的 diff 向量保持与输入向量相同的大小(也许结果向量将用于另一个例程假设输入向量长度进行计算,如果没有其余代码,这很困难),希望它有所帮助

于 2012-07-10T03:18:01.573 回答