我想你需要
m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[]);
注意,此讀取Matlab的標準矩陣,column-major訂單(下降,然後在)。
然後你就可以轉換回來
b_recovered = reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b));
由於typecast
轉換數據類型在不改變基礎數據,這個過程需要不會損失精度。例如,
>> b = randn(2,3)
b =
-0.241247174335006 0.540703471823211 0.526269662140438
0.908207564087271 -0.507829312416083 -1.067884765919437
>> m = reshape(dec2bin(typecast(b(:),'uint8'),8).',1,[])
m =
101100101011100000000010111110100010111111100001110011101011111101001010100000100011011101001111000010010001000011101101001111110010100100001111000010100101111001110001010011011110000100111111001011101101111100011000010000100010001101000000111000001011111100010010101001010111100001111001001100111101011111100000001111110101110001010100000110000101011000001110000101101111000110111111
>> b_recovered = reshape(typecast(uint8(bin2dec(reshape(m,8,[]).')),'double'),size(b))
b_recovered =
-0.241247174335006 0.540703471823211 0.526269662140438
0.908207564087271 -0.507829312416083 -1.067884765919437
>> b==b_recovered
ans =
2×3 logical array
1 1 1
1 1 1
>>
我想你需要'米=重塑(DEC2BIN(類型轉換(二(:), 'UINT8'),8)。',1,[])',然後用'重塑轉換回(如果從此註釋中粘貼,請注意可能不需要的零寬度字符最後一個逗號) –
@LuisMendo感謝您的回答。但有錯誤'輸入字符在MATLAB語句或表達式中無效'。 – Hanna
正如我在之前的評論中所述,某些不需要的零寬度字符以某種方式蠕變。我已經發布了它作爲答案,這可以避免那 –