什么是R/Python/Julia中Matlab的类型转换功能的等价物

uda*_*day 1 python matlab r julia

什么是Matlab typecast在R中的功能?在Python?在朱莉娅?这里给出了Matlab的类型转换函数的描述:类型转换

例如,在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
Run Code Online (Sandbox Code Playgroud)

谢谢

请注意:我不是在寻找R/Python的/朱莉娅相当于Matlab的的cast功能(例如我不找as.integer,as.character在R)

编辑:

感谢Julia/R/Python的答案.StackOverflow允许我选择一个答案,但我投了所有答案.

Ken*_*her 10

在朱莉娅,你正在寻找重新解释:

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
Run Code Online (Sandbox Code Playgroud)

但请注意,对于矩阵,即使第一个维度是单个,也需要指定结果维度(因为不管是否需要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
Run Code Online (Sandbox Code Playgroud)


Spa*_*man 7

在R中,您可以将对象写入原始二进制连接并获取字节向量.这将使您获得与uint8输出示例相同的内容:

> X=c(1,255,256)
> mode(X)
[1] "numeric"
Run Code Online (Sandbox Code Playgroud)

这里重要的是存储模式,而不是模式.所以我们将它设置为整数 - 这相当于uint32,即每个整数4个字节:

> storage.mode(X)
[1] "double"
> storage.mode(X)="integer"
Run Code Online (Sandbox Code Playgroud)

现在我们可以使用了writeBin.第二个参数是一个任意的原始向量,因此我们创建一个长度为零的ad hoc.我们只关心返回值:

> Xraw = writeBin(X,raw(0))
> Xraw
 [1] 01 00 00 00 ff 00 00 00 00 01 00 00
Run Code Online (Sandbox Code Playgroud)

反过来使用readBin:

> readBin(Xraw,"int",n=3)
[1]   1 255 256
Run Code Online (Sandbox Code Playgroud)

将前8个字节解压缩为double:

> Xdoub = readBin(Xraw,"double",n=1)
> Xdoub
[1] 5.411089e-312
Run Code Online (Sandbox Code Playgroud)

显然是一个无意义的价值观.但是让我们检查它的前8个字节:

> writeBin(Xdoub,raw(0))
[1] 01 00 00 00 ff 00 00 00
Run Code Online (Sandbox Code Playgroud)

R并不真正具有所有C级类型,因此如果您需要任何内容​​,您可以从原始字节构建它或编写一些C代码以与R函数链接.


Amr*_*mro 6

蟒蛇/麻木:

>>> import numpy as np
>>> x = np.array([1,255,256], dtype=np.int32)
>>> y = x.view(np.uint8)
Run Code Online (Sandbox Code Playgroud)

(您可以类似地更改x自身的类型:)x.dtype = np.uint8

输出:

>>> x
array([  1, 255, 256])
>>> y
array([  1,   0,   0,   0, 255,   0,   0,   0,   0,   1,   0,   0], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

注意,y就地重新解释视图x,因此在任何变化y将在被反射x

>>> y[:] = 255
>>> x
array([-1, -1, -1])
Run Code Online (Sandbox Code Playgroud)

MATLAB

这是等效的 MATLAB 输出:

>> x = int32([1,2,3])
x =
           1           2           3
>> y = typecast(x, 'uint8')
y =
    1    0    0    0    2    0    0    0    3    0    0    0

>> y(:) = 255
y =
  255  255  255  255  255  255  255  255  255  255  255  255
>> xx = typecast(y, 'int32')
xx =
          -1          -1          -1
Run Code Online (Sandbox Code Playgroud)

如果您想要类型转换而不在 MATLAB 中创建深层副本,请参阅typecastxMEX 函数(使用未记录的功能来创建共享数据副本)。


请注意 MATLAB 使用饱和算法,取消链接具有模块化算法的Python :

蟒蛇/麻木

# wraps around the other end
>>> np.array(257, dtype=np.uint8)
array(1, dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

MATLAB

% saturates at the maximum
>> uint8(257)
ans =
  255
Run Code Online (Sandbox Code Playgroud)