我有以下问题,
使用 Matlab 我有一个非常大的向量包含让我们说的1和0,我想将它转换为十进制,事情是数字是大的所以变量不能握住它,所以我想把它制成可接受边界内的小块并将这些块转换成十进制数,然后存储在矩阵或向量中。我想知道你是否对如何实现这个或更好的方法有任何建议。
提前致谢。
答案 0 :(得分:3)
<强>代码强>
%%// Given binary number
comp = '1010101011010110010010101001';
decnum_tobe_stored = bin2dec(comp) %%// To be used for verification
%%// Parameters
ndigits = 4; %%// Number of binary digits used per element for storage.
%%// Storing Process
app1 = ndigits - rem(numel(comp),ndigits);
comp = [repmat('0',[1 app1]) comp];
bin_array = reshape(comp,ndigits,[])' %%//' This is your binary storage medium
dec_array = bin2dec(bin_array) %%// This is your decimal storage medium
%%// Retrieving Process
pf = fliplr(ndigits.*(0:size(bin_array,1)-1));
v2 = power(2,pf)';%//'
ret_number = sum(dec_array.*v2); %%// Retrieved number
%%// Verfication
check_error = isequal(ret_number,decnum_tobe_stored) %%// org_number must be same as verify_decnum for this technique
<强>输出强>
decnum_tobe_stored =
179135657
bin_array =
0000
1010
1010
1101
0110
0100
1010
1001
dec_array =
0
10
10
13
6
4
10
9
check_error =
1
更大案例
我们假设您有一个二进制数 -
comp = '10101010110101100100101010011010101101010101010101101011001111'
您会知道bin2dec
允许最大二进制数位为52
的能力限制您,因此您可以使用ndigits = 52
。运行代码后,您的十进制存储阵列将具有此分区数据 -
dec_array =
683
1.5686e+15
如果您启用了format longe
,则可以轻松地显示整个数据。因此,
dec_array =
6.830000000000000e+02
1.568619453831887e+15
贪婪的存储空间:如果您真的想将大量数据存入非常小的存储阵列,这是适用的。
<强>代码强>
%%// Given binary number
comp = ['10101010110101100100101010011010101101010101010101101011001001' ...
'01010011010101011010101010101101011001001010100110101010110101' ...
'01010110110101011010110010010101001101010101101010101010110101' ...
'10010010101001101010101101010100110010010101001101010101101010' ...
'10101011010110010010101001101010101101010101100100101010011010' ...
'10101101010101010110101100100101010011010101011010101010101010' ...
'11010101010101101011001001010100110101010110101010101010110101' ...
'10010010101001101010101101010101010110101100100101010011010101' ...
'01101010101010101101011001001010100110101010110101010101011010' ...
'11001001010100110101010110101010101011010110010010101001101010' ...
'10110101010101011010110010010101001101010101101011001001010100' ...
'11010101011010110010010101001101010101101011001001010100110101' ...
'01011010110010010101001101010101101011001001010100110101010110' ...
'10110010010101001101010101101011001001010100110101010110101100' ...
'10010101001101010101101011001001010100110101010110101100100101' ...
'01001101010101101011001001010100110101010110101100100101010011' ...
'11010101010101101011001001010100110101010110101010101010110101' ...
'10010010101001101010101101010101010110101100100101010011010101' ...
'01101010101010101101011001001010100110101010110101010101011010' ...
'11001001010100110101010110101010101011010110010010101001101010' ...
'10110101010101011010110010010101001101010101101011001001010100' ...
'11010101011010110010010101001101010101101011001001010100110101' ...
'01011010110010010101001101010101101011001001010100110101010110' ...
'10110010010101001101010101101011001001010100110101010110101100' ...
'0101010110101100100101010011010101011010110010010101001'];
%%// Parameters
ndigits = 52; %%// Number of digits allowed
%%// Storing Process
app1 = ndigits - rem(numel(comp),ndigits);
comp = [repmat('0',[1 app1]) comp];
bin_array = reshape(comp,ndigits,[])'; %%//' This is your binary storage medium
dec_array = bin2dec(bin_array) %%// This is your decimal storage medium
%%// ******** Greedy storage approach ******************
%%// Maximum number of elements that can be packed together
num_ele_cumsum = find(isinf(cumsum(power(1E16,1:24))),1)-1;
dec_array = [zeros(num_ele_cumsum - rem(numel(dec_array),num_ele_cumsum),1) ;dec_array];
dec_array_mat = reshape(dec_array,num_ele_cumsum,[]);
exp_nums = repmat(power(1E16,num_ele_cumsum-1:-1:0)',[1 size(dec_array_mat,2)]);%%//'
cumsum_vals = cumsum(dec_array_mat.*exp_nums,1);
dec_array_compact = cumsum_vals(end,:)'
<强>输出强>
dec_array =
2.2929e+10
3.0024e+15
1.5971e+15
.......
2.7211e+15
3.0504e+15 (38 rows)
dec_array_compact =
2.2929e+170
1.5686e+303
另请注意,检索过程(此处未列出)与编码过程一样复杂。所以,在我认为极其有限的内存限制的情况下,这是有意义的。