1
$\begingroup$

Assume you have a list of 5 numbers, eg L = {14, 7, 11, 14, 0};.

Now, what you want to do with this list, is to transform each entry into eg a binary list, using IntegerDigits[L, 2, 5] and then flatten out that matrix, to get something like BL = Flatten[IntegerDigits[L, 2, 5]] (the last argument in IntegerDigits does not have to be 5, it can be anything).

BL is a binary list with 25 (= 5 numbers x 5 bits per entry) 0-1 entries.

What would be an efficient way to perform that transformation, but for a matrix of entries eg M = {{14, 7, 11, 14, 0}, {1, 15, 12, 8, 15}, {3, 11, 1, 1, 5}}.

Would the solution proposed above be similarly 'efficient' if that process were to be repeated for a given number of times eg with 5 different M matrices (again 5 is arbitrary here)?

Please note that it is not possible to have all the M matrices available at once; it would have to be sequential.

My current implementation is this

BlockRandom[
   (* quick way to get 'ranges' for use in 'DiscreteUniformDistribution' *)
   ranges = Sort /@ RandomInteger[{0, 100}, {5, 2}];

   Map[
    (* sequentially transform each matrix *)
    (Flatten /@ IntegerDigits[#, 2, 5]) &,

    (* generate 5 matrices *)
    RandomVariate[DiscreteUniformDistribution[ranges], {5, 5}]
   ],

   RandomSeeding -> 123456789
 ]
$\endgroup$
  • $\begingroup$ So, Flatten[IntegerDigits[M, 2, 5], {{1}, {2, 3}}]? $\endgroup$ – J. M. is away Nov 3 '17 at 8:04
  • $\begingroup$ @J.M. sure; everything else seems about right? $\endgroup$ – user42582 Nov 3 '17 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.