相当于Matlab的什么typecast
R 中的函数?在Python中?在朱莉娅? Matlab的typecast函数的描述如下:typecast http://www.mathworks.com/help/matlab/ref/typecast.html
示例,在 Matlab 中
X = uint32([1 255 256])
X =
1 255 256
Y = typecast(X, 'uint8') # little endian
Y =
1 0 0 0 255 0 0 0 0 1 0 0
Thanks
请注意:我并不是在寻找与 Matlab 等效的 R / Python / Juliacast
函数(例如我不是在寻找as.integer
, as.character
in R)
EDIT:
感谢 Julia / R / Python 的回答。 StackOverflow 允许我选择一个答案,但我对所有答案都投了赞成票。
在朱莉娅,您正在寻找重新诠释:
julia> X = Uint32[1,255,256]
3-element Array{Uint32,1}:
0x00000001
0x000000ff
0x00000100
julia> Y = reinterpret(Uint8,X)
12-element Array{Uint8,1}:
0x01
0x00
0x00
0x00
0xff
0x00
0x00
0x00
0x00
0x01
0x00
0x00
但请注意,对于矩阵,即使第一个维度是单维,您也需要指定结果维度(因为您想要 4x3 还是 1x12 数组是不明确的):
julia> X = Uint32[1 255 256]
1x3 Array{Uint32,2}:
0x00000001 0x000000ff 0x00000100
julia> Y = reinterpret(Uint8,X) # This won't work
ERROR: result shape not specified
julia> Y = reinterpret(Uint8,X,(1,12))
1x12 Array{Uint8,2}:
0x01 0x00 0x00 0x00 0xff 0x00 0x00 0x00 0x00 0x01 0x00 0x00
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)