4
$\begingroup$

I want to "Mark" some elements in a list if they obey certain conditions. I honestly do not know if there is a way to mark elements, so I have decided to create another list where those numbers which obey the criteria will be represented in "1" (And those which do not will be represented in "0").

The condition is Divisable[2*#-1,3]

Here is the code, which is not working for a reason.

a = 12
blist = 2*Range[a/2] - 2
enter code here
ilist = Table[0, Length[blist]]
Map[If[Divisable[2*# - 1, 3],ReplacePart[ilist, Position[blist, #] -> 1]] &, blist]
ilist

The list blist ({0,2,4,6,8,10}) has two elements that meet the criteria, and therefore the final output of ilist should be the following:

{0,1,0,0,1,0}

However, Mathematica outputs the following:

{0,0,0,0,0,0}

If you could tell me what is wrong with my code and how to properly "Mark" certain elements in a list, I'd be very grateful. Any help is appreciated.

$\endgroup$
  • 1
    $\begingroup$ Try Boole[Divisible[2 # - 1, 3]] & /@ blist. $\endgroup$ – J. M.'s technical difficulties Jul 27 '17 at 10:23
  • $\begingroup$ Thanks, it works well! $\endgroup$ – user51325 Jul 27 '17 at 10:26
  • $\begingroup$ Not as neat as JM's comment, but using your own approach: If[Divisible[2*# - 1, 3], 1, 0] & /@ {0, 2, 4, 6, 8, 10} $\endgroup$ – user1066 Jul 27 '17 at 10:34
  • $\begingroup$ Works just as fine! $\endgroup$ – user51325 Jul 27 '17 at 10:35
  • 3
    $\begingroup$ ilist = Boole[Divisible[2 blist - 1, 3]] $\endgroup$ – Coolwater Jul 27 '17 at 11:12
1
$\begingroup$
 blist = 2*Range[12/2] - 2; 

 -Mod[2 blist - 1, 3] // UnitStep

{0, 1, 0, 0, 1, 0}

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much! $\endgroup$ – user51325 Jul 27 '17 at 10:49
  • $\begingroup$ You're welcome. You could define blist with Range[1, 2 a, 4] - 2 and then just -Mod[blist, 3] // UnitStep $\endgroup$ – eldo Jul 27 '17 at 11:02
0
$\begingroup$
blist = {0, 2, 4, 6, 8, 10};
1 - Unitize @ Mod[2 blist - 1, 3]  

{0, 1, 0, 0, 1, 0}

| improve this answer | |
$\endgroup$

Your Answer

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