查看单个帖子
旧 2019-12-10, 20:30   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 在Octave / Matlab中类型转换为int

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

Time[V0Found] 其中V0Found类似于5.2,采用索引5足够接近,因此我需要删除小数。我用这个方程式去掉了小数:

V0FoundDec = V0Found - mod(V0Found,1) Time[V0FoundDec] 但是,即使删除了小数,八度仍然会抱怨它。

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



回答:

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

但令我惊讶的是,您需要为索引执行此操作。尝试

myarray(floor(indexlist)) 要么

myarray(round(indexlist)) 其中myarray是您的数组,而indexlist是您的非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

更多&回答...
poster 当前离线   回复时引用此帖