Message Boards Message Boards

[?] Rearrange the following binary data?

The data is given by:

q = Flatten[
  Table[{a, b, c, d}, {a, 0, 1}, {b, 0, 1}, {c, 0, 1}, {d, 0, 1}], 3].
Mathematica gives the following order:
{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 
  0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 
  0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 
  1, 1, 0}, {1, 1, 1, 1}};

I would like the data to be ordered as below (i.e. the order should be, 2nd, 4th, 6th,8th, 10th, etc):

newdata4 = {{0, 0, 0, 1}, {0, 0, 1, 1}, {0, 1, 0, 1}, {0, 1, 1, 
    1}, {1, 0, 0, 1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {1, 1, 1, 1}, {0, 0,
     0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 
    0}, {1, 0, 1, 0}, {1, 1, 0, 0}, {1, 1, 1, 0}};

Can anyone help me? Im fact I would like to use a much larger combination of binaries and the documentation does not help!

6 Replies

Could join evens to odds, as below.

qEvenOdd = Join[q[[2 ;; -1 ;; 2]], q[[1 ;; -2 ;; 2]]]

(* {{0, 0, 0, 1}, {0, 0, 1, 1}, {0, 1, 0, 1}, {0, 1, 1, 1}, {1, 0, 0, 
  1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 
  1, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 0}, {1, 0, 1, 0}, {1, 
  1, 0, 0}, {1, 1, 1, 0}} *)
POSTED BY: Daniel Lichtblau

Is it this what you really want? :

IntegerDigits[#, 2, 4] & /@ SortBy[Range[0, 15], EvenQ]

Or you can generalize:

oddEvenDigits[max_Integer] := IntegerDigits[#, 2, Ceiling[Log[max]/Log[2.]]] & /@ SortBy[Range[0, max], EvenQ]
POSTED BY: Henrik Schachner

Thanks very much Daniel!!! I read very often your posts and comments and appreciate it.

Excellent Henrik! But I think the last part is: IntegerDigits[#, 2, Ceiling[Log[max]/Log[2.]]] & /@ SortBy[Range[0, max - 1], EvenQ]; otherwise it adds one more with zeros.

oddEvenDigits[15]
    (* Out:   {{0,0,0,1},{0,0,1,1},{0,1,0,1},{0,1,1,1},{1,0,0,1},{1,0,1,1},{1,1,0,1}\
    ,{1,1,1,1},{0,0,0,0},{0,0,1,0},{0,1,0,0},{0,1,1,0},{1,0,0,0},{1,0,1,0}\
    ,{1,1,0,0},{1,1,1,0}}   *)

... I did not regard max (i.e. the argument of my oddEvenDigits) as a power of 2. It can be just any number.

For the case of "all combination" write

oddEvenDigits[2^n - 1]
POSTED BY: Henrik Schachner

Thanks very much Henrik! I appreciate your time. This is exactly what I did when I regarded your formulation as (max - 1), because you get for instance oddEvenDigits[31] if n = 5.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract