在matlab中连接二进制数据?
Concatenate binary data in matlab?
我想通过连接 两个 4 位 值和 一个 2 来创建一个 10 位 二进制值-bit 值。
例如:{2'b11,4'b1010,4'b1100}.
我该如何实现?
- strcat(bin2b,bin4b,bin4b) 结果与串联,但这个结果将被 Mlab 视为 STRING。
据我所知,最好的解决方法是编写一个在 C/C++/whatever 中执行此操作的函数,然后使用 MEX API 从 Matlab 调用它。
Matlab 不允许您以这种方式处理实际上不支持的类型,或者如果您真的想在 Matlab 中执行它,它将非常丑陋和缓慢。
有很多选项,具体取决于您想要的最终结果:
bin2b = '11' ;
bin4b1 = '1010' ;
bin4b2 = '1100' ;
b10str = strcat(bin2b,bin4b1,bin4b2) %// 10 bit value as a string type
b10str = [bin2b bin4b1 bin4b2] %// 10 bit value as a string type (same than above, shorthand notation for concatenation)
b10dec = bin2dec(b10str) %// 10 bit value as a numeric type (decimal base)
b10hex = dec2hex(bin2dec(b10str)) %// string type again (hexadecimal base)
b10bitarray = de2bi(b10dec) %// array of 10 boolean (each represent one bit)
那会给你:
b10str =
1110101100
b10dec =
940
b10hex =
3AC
b10bitarray =
0 0 1 1 0 1 0 1 1 1
注意:在大多数 PC 上,二进制排序为 "lower endian"。根据字节顺序,您可能希望在转换之前 "flip" 您的位数组,这可以通过 fliplr
:
完成
>> fliplr(b10str)
ans =
0011010111
然后如上转换
我想通过连接 两个 4 位 值和 一个 2 来创建一个 10 位 二进制值-bit 值。 例如:{2'b11,4'b1010,4'b1100}.
我该如何实现?
- strcat(bin2b,bin4b,bin4b) 结果与串联,但这个结果将被 Mlab 视为 STRING。
据我所知,最好的解决方法是编写一个在 C/C++/whatever 中执行此操作的函数,然后使用 MEX API 从 Matlab 调用它。
Matlab 不允许您以这种方式处理实际上不支持的类型,或者如果您真的想在 Matlab 中执行它,它将非常丑陋和缓慢。
有很多选项,具体取决于您想要的最终结果:
bin2b = '11' ;
bin4b1 = '1010' ;
bin4b2 = '1100' ;
b10str = strcat(bin2b,bin4b1,bin4b2) %// 10 bit value as a string type
b10str = [bin2b bin4b1 bin4b2] %// 10 bit value as a string type (same than above, shorthand notation for concatenation)
b10dec = bin2dec(b10str) %// 10 bit value as a numeric type (decimal base)
b10hex = dec2hex(bin2dec(b10str)) %// string type again (hexadecimal base)
b10bitarray = de2bi(b10dec) %// array of 10 boolean (each represent one bit)
那会给你:
b10str =
1110101100
b10dec =
940
b10hex =
3AC
b10bitarray =
0 0 1 1 0 1 0 1 1 1
注意:在大多数 PC 上,二进制排序为 "lower endian"。根据字节顺序,您可能希望在转换之前 "flip" 您的位数组,这可以通过 fliplr
:
>> fliplr(b10str)
ans =
0011010111
然后如上转换