[英]Typecast to an int in Octave/Matlab
I need to call the index of a matrix made using the linspace command, and based on some data taken from an oscilloscope.我需要调用使用 linspace 命令创建的矩阵的索引,并基于从示波器获取的一些数据。 Because of this, the data inputed is a double.因此,输入的数据是双精度的。 However, I can't really call:但是,我真的不能打电话:
Time[V0Found]
where V0Found is something like 5.2 however, taking index 5 is close enough, so I need to drop the decimal.其中 V0Found 类似于 5.2 但是,取索引 5 已经足够接近了,所以我需要去掉小数点。 I used this equation to drop the decimal:我用这个等式去掉小数:
V0FoundDec = V0Found - mod(V0Found,1)
Time[V0FoundDec]
However, even though that drops the decimal, octave still complains about it.然而,即使这会降低小数点,Octave 仍然会抱怨它。
So, what can I do to typecast it to an int?那么,我该怎么做才能将其类型转换为 int 呢?
In MATLAB, it should be int8(x)
or int16(x)
or one of the other integer casts . 在MATLAB中,它应该是int8(x)
或int16(x)
或其他整数强制转换之一 。
But I'm surprised you need to do that for an index. 但令我惊讶的是,您需要为索引执行此操作。 Try 尝试
myarray(floor(indexlist))
or 要么
myarray(round(indexlist))
where myarray
is your array and indexlist
is your vector of noninteger indices. 其中myarray
是您的数组,而indexlist
是您的非indexlist
索引的向量。
example: 例:
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
You could use round , floor , ceil functions instead of your formula to do the rounding. 您可以使用round , floor , ceil函数而不是公式来进行舍入。
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 如果那是你的问题
In matlab, the right way to do this is to use the interp1 command to interpolate. 在matlab中,正确的方法是使用interp1命令进行插值。 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) yout = interp1(xdata,ydata,xin,...)或yout = interp1(ydata,xin,...)其中xdata假定为1:length(ydata)
If you want to produce the result you would get from calling 如果要产生结果,您可以通过致电获得
V0FoundDec = Time(round(V0found)) V0FoundDec =时间(回合(V0found))
you would say 你会说
V0FoundDec = interp1(Time, V0found, 'nearest') V0FoundDec = interp1(时间,V0found,'最近')
but you can just as easily get linear interpolation (this is the default) with 但是您可以轻松地通过以下方法获得线性插值(这是默认设置)
V0FoundDec = interp1(Time, V0found) V0FoundDec = interp1(时间,V0found)
or 要么
V0FoundDec = interp1(Time,V0found,'linear') V0FoundDec = interp1(时间,V0found,'线性')
and you can also extrapolate outside the limits (using 'extrap' or providing an extrap value), where 您还可以在限制之外进行推断(使用“ extrap”或提供Extrap值),其中
Time(round(V0found)) 时间(回合(V0found))
will crash if round(V0found) < 1 or > length(Time) 如果round(V0found)<1或> length(Time)将崩溃
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.