2015-04-22 558 views
0

我想創建一個10位二進制值通過連接兩個4位值和一個2位值。例如:{2'b11,4'b1010,4'b1100}在matlab中連接二進制數據?

我該怎麼做到這一點?

  • 的strcat(bin2b,bin4b,bin4b)結果與級聯,但是這產生將被視爲由MLAB STRING

回答

1

相當多的選項有具體取決於您希望您的最終結果:

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,二進制排序是「下端「。根據字節序,你可能要「翻轉」轉換之前你的位陣列,可以用fliplr做到:

>> fliplr(b10str) 
ans = 
0011010111 

,然後再轉換爲上述

+0

感謝您的解決方案。 – Manojkumar

+0

對於重新排序位,我們可以使用 de2bi(b10dec,'left-msb') – Manojkumar

+0

@Manjkumar。這是完全正確的。但它只適用於這個特定的功能/格式。如果那是你使用的,那很好。 'fliplr'選項更通用,因爲它可以處理任何類型的數組(布爾,數字,字符等)。 – Hoki

0

據我所知最好的解決辦法是編寫一個函數,在C/C++/whatever中執行此操作,並使用MEX API從Matlab調用它。

Matlab不會讓你處理那種真正不被支持的類型,或者你真的想在Matlab中做到這一點,它會非常醜陋和緩慢。