Domanda

I have the following question,

working on Matlab I have a very large vector containing lets say ones and zeros, and I want to convert it into decimal, the thing is that the number is way to big so a variable can not hold it, so I thought braking it into small pieces that are within the acceptable boundaries and converting those pieces into decimal numbers that will be stored in a matrix or vector. I was wondering if you have any suggestions on how to implement that or a better way all together.

Thanks in advance.

È stato utile?

Soluzione

Code

%%// 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

Output

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

Bigger case

Let's suppose you have a binary number as this -

comp = '10101010110101100100101010011010101101010101010101101011001111'

You would know that you are limited by bin2dec's capability of allowing a maximum number of binary digits as 52, so you can use ndigits = 52. After running the code, your decimal storage array would have this partitioned data -

dec_array =
          683
   1.5686e+15

You can easily visualize the entire data if you have format longe enabled. Thus,

dec_array =

     6.830000000000000e+02
     1.568619453831887e+15

Greedy storage case: This is applicable if you really want to squeeze in a lot of data into a very small storage array.

Code

%%// 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,:)'

Output

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

Also, note that the retrieving process (not listed here) would be as complicated as the encoding process. So, this would make sense in an extremely memory-limited case I would think.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top