# [✓] Rearrange the following binary data?

Posted 1 year ago
1449 Views
|
6 Replies
|
2 Total Likes
|
 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
Sort By:
Posted 1 year ago
 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 1 year ago
Posted 1 year ago
 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 1 year ago
 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]