3

我需要调用使用 linspace 命令创建的矩阵的索引,并基于从示波器获取的一些数据。因此,输入的数据是双精度的。但是,我真的不能打电话:

Time[V0Found]

其中 V0Found 类似于 5.2 但是,采用索引 5 已经足够接近了,所以我需要去掉小数点。我用这个等式去掉小数点:

V0FoundDec = V0Found - mod(V0Found,1)
Time[V0FoundDec]

然而,即使这会减少小数点,八度仍然会抱怨它。

那么,我该怎么做才能将其类型转换为 int?

4

3 回答 3

6

在 MATLAB 中,它应该是int8(x)orint16(x)其他整数强制转换之一

但我很惊讶您需要为索引执行此操作。尝试

myarray(floor(indexlist))

或者

myarray(round(indexlist))

myarray你的数组在哪里,indexlist你的非整数索引向量在哪里。


例子:

octave-3.2.3:8> v=rand(1,8)*10+1
v =

   3.1769   1.4397   8.7504   1.7424   6.9413   3.1663   8.4085   9.0179

octave-3.2.3:9> a = (1:1:20).^2
a =

 Columns 1 through 15:

     1     4     9    16    25    36    49    64    81   100   121   144   169   196   225

 Columns 16 through 20:

   256   289   324   361   400

octave-3.2.3:10> a(floor(v))
ans =

    9    1   64    1   36    9   64   81
于 2010-03-31T22:15:40.823 回答
2

You could use round, floor, ceil functions instead of your formula to do the rounding.

By the way, indexing is done using parenthesis instead of brackets so:

V0FoundDec = round(V0Found);
Time(V0FoundDec) % not Time[V0FoundDec]

In case that was your problem

于 2010-03-31T22:16:51.290 回答
1

In matlab, the right way to do this is to use the interp1 command to interpolate. The format of this command is

yout = interp1 (xdata, ydata, xin, ...) or yout = interp1 (ydata, xin, ...) where xdata is then assumed to be 1:length(ydata)

If you want to produce the result you would get from calling

V0FoundDec = Time(round(V0found))

you would say

V0FoundDec = interp1(Time, V0found, 'nearest')

but you can just as easily get linear interpolation (this is the default) with

V0FoundDec = interp1(Time, V0found)

or

V0FoundDec = interp1(Time,V0found,'linear')

and you can also extrapolate outside the limits (using 'extrap' or providing an extrap value), where

Time(round(V0found))

will crash if round(V0found) < 1 or > length(Time)

于 2010-04-02T02:25:08.100 回答