3
$\begingroup$

We have a list containing IntegerDigits elements as

list0={{0,0,1,0,0,0},
 {0,0,1,0,1,0},
 {0,0,1,1,0,0},
 {0,0,1,1,1,0},
 {0,1,1,0,0,0},
 {0,1,1,0,1,0},
 {0,1,1,1,1,0},
 {1,0,1,0,0,0},
  ...};

Two any arbitrary positions of each element can be definitely determined by 0 or 1. For instants, in the above list, we have 0 in the first position and 1 in the fourth position. They are fixed and are not changeable. We have to create a list whose elements are -1 or +1. If the Total between two mentioned positions isEven (if evenQ=True) it must be +1, otherwise it must be -1. Or we can say (-1)^(Total[elements between first and fourth positions]) I mean:

list1={{1}, (*0+0=0*)
       {-1}, (*0+1=1*)
       {-1}, (*1+0=1*)
       {+1},  (*1+1=2*)
       {+1},
       {-1},
       {+1},
       {+1},
       ....}

How can I do this aim?

$\endgroup$
  • $\begingroup$ Why do you write (*0+0=0*) for the first one? It has 1 in the 3rd position, and 0 in the 6th position. $\endgroup$ – Coolwater Aug 17 '17 at 16:23
  • $\begingroup$ Should the position be taken backwards because IntegerDigits puts the coefficient of the smallest power of the base at the end of the output list $\endgroup$ – Coolwater Aug 17 '17 at 16:25
  • 2
    $\begingroup$ Something like (-1)^Total[list0[[All, {4, 5}]], {2}]? $\endgroup$ – Carl Woll Aug 17 '17 at 16:26
  • $\begingroup$ For your second comment you are right. So sorry. But for the first comment: there are two 0s between 1 and 0. $\endgroup$ – Inzo Babaria Aug 17 '17 at 16:27
  • $\begingroup$ So sorry I corrected them. $\endgroup$ – Inzo Babaria Aug 17 '17 at 16:29
2
$\begingroup$
list0 =
 {{0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 1, 0}, {0, 0, 1, 1, 0, 0},
  {0, 0, 1, 1, 1, 0}, {0, 1, 1, 0, 0, 0}, {0, 1, 1, 0, 1, 0},
  {0,1, 1, 1, 1, 0}, {1, 0, 1, 0, 0, 0}};

If[#, 1, -1]& @* EvenQ @* Total /@ list0[[All, 4 ;; 5]]

{1, -1, -1, 1, 1, -1, 1, 1}

Or, similar to Carl Woll's comment,

(-1)^(Plus @@@ list0[[All, 4 ;; 5]])
$\endgroup$
  • $\begingroup$ Thank you so much but because the positions are arbitrary the Total maybe exceeds from 2 or 0 $\endgroup$ – Inzo Babaria Aug 17 '17 at 16:31
  • $\begingroup$ See updated answer $\endgroup$ – eldo Aug 17 '17 at 16:41
2
$\begingroup$

I suspect you started out with the integers themselves:

ints = FromDigits[#, 2] & /@ list0

{8, 10, 12, 14, 24, 26, 30, 40}

If that's the case, then you don't need IntegerDigits:

(-1)^Sign[Mod[BitAnd[2^1 + 2^2, #], 2^1 + 2^2]] & /@ ints 
(-1)^Sign[Mod[BitAnd[6, #], 6]] & /@ ints 

{1, -1, -1, 1, 1, -1, 1, 1}

where 2^0 corresponds to the last element of IntegerDigits and 2^1 corresponds to the second last, etc.

$\endgroup$
  • $\begingroup$ What is dd? It was not defined! $\endgroup$ – Inzo Babaria Aug 17 '17 at 16:34
  • $\begingroup$ @InzoBabaria I removed them $\endgroup$ – Coolwater Aug 17 '17 at 16:35
  • $\begingroup$ I cannot understand why 6?! $\endgroup$ – Inzo Babaria Aug 17 '17 at 16:39
  • $\begingroup$ I cannot generalize to an arbitrary case $\endgroup$ – Inzo Babaria Aug 17 '17 at 16:39
  • 2
    $\begingroup$ You might be interested in BitGet[]. $\endgroup$ – J. M. will be back soon Aug 17 '17 at 16:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.