2
$\begingroup$
 c[i_, j_] := If[OddQ[i + j] == True, Ti, Ni];
l1 = Flatten[
  Table[Flatten[{i b[[1]] + j b[[2]], c[i, j]}, 1], {i, -3, 
    3}, {j, -3, 3}], 1]
(* {{3, 0, Ni}, {3, -1, Ti}, {3, -2, Ni}, {3, -3, Ti}, {3, -4, 
  Ni}, {3, -5, Ti}, {3, -6, Ni}, {2, 1, Ti}, {2, 0, Ni}, {2, -1, 
  Ti}, {2, -2, Ni}, {2, -3, Ti}, {2, -4, Ni}, {2, -5, Ti}, {1, 2, 
  Ni}, {1, 1, Ti}, {1, 0, Ni}, {1, -1, Ti}, {1, -2, Ni}, {1, -3, 
  Ti}, {1, -4, Ni}, {0, 3, Ti}, {0, 2, Ni}, {0, 1, Ti}, {0, 0, 
  Ni}, {0, -1, Ti}, {0, -2, Ni}, {0, -3, Ti}, {-1, 4, Ni}, {-1, 3, 
  Ti}, {-1, 2, Ni}, {-1, 1, Ti}, {-1, 0, Ni}, {-1, -1, Ti}, {-1, -2, 
  Ni}, {-2, 5, Ti}, {-2, 4, Ni}, {-2, 3, Ti}, {-2, 2, Ni}, {-2, 1, 
  Ti}, {-2, 0, Ni}, {-2, -1, Ti}, {-3, 6, Ni}, {-3, 5, Ti}, {-3, 4, 
  Ni}, {-3, 3, Ti}, {-3, 2, Ni}, {-3, 1, Ti}, {-3, 0, Ni}} *)

In the above code, I want to Randomly chose n number (say 5) elements from the list l1 which contain Ni as replace that Ni by O. How do I do it?

$\endgroup$

1 Answer 1

4
$\begingroup$

The goal here is to use Position to make a list of the positions where Ni appears in the list, and then use RandomChoice to choose 5 and replace them using ReplacePart

list = {{3, 0, Ni}, {3, -1, Ti}, {3, -2, Ni}, {3, -3, Ti}, {3, -4, 
    Ni}, {3, -5, Ti}, {3, -6, Ni}, {2, 1, Ti}, {2, 0, Ni}, {2, -1, 
    Ti}, {2, -2, Ni}, {2, -3, Ti}, {2, -4, Ni}, {2, -5, Ti}, {1, 2, 
    Ni}, {1, 1, Ti}, {1, 0, Ni}, {1, -1, Ti}, {1, -2, Ni}, {1, -3, 
    Ti}, {1, -4, Ni}, {0, 3, Ti}, {0, 2, Ni}, {0, 1, Ti}, {0, 0, 
    Ni}, {0, -1, Ti}, {0, -2, Ni}, {0, -3, Ti}, {-1, 4, Ni}, {-1, 3, 
    Ti}, {-1, 2, Ni}, {-1, 1, Ti}, {-1, 0, Ni}, {-1, -1, Ti}, {-1, -2,
     Ni}, {-2, 5, Ti}, {-2, 4, Ni}, {-2, 3, Ti}, {-2, 2, Ni}, {-2, 1, 
    Ti}, {-2, 0, Ni}, {-2, -1, Ti}, {-3, 6, Ni}, {-3, 5, Ti}, {-3, 4, 
    Ni}, {-3, 3, Ti}, {-3, 2, Ni}, {-3, 1, Ti}, {-3, 0, Ni}};

ReplacePart[list,
 RandomChoice[Position[list, Ni], 5] -> O]
(* {{3, 0, Ni}, {3, -1, Ti}, {3, -2, Ni}, {3, -3, Ti}, {3, -4, 
  Ni}, {3, -5, Ti}, {3, -6, O}, {2, 1, Ti}, {2, 0, Ni}, {2, -1, 
  Ti}, {2, -2, Ni}, {2, -3, Ti}, {2, -4, O}, {2, -5, Ti}, {1, 2, 
  Ni}, {1, 1, Ti}, {1, 0, Ni}, {1, -1, Ti}, {1, -2, Ni}, {1, -3, 
  Ti}, {1, -4, Ni}, {0, 3, Ti}, {0, 2, O}, {0, 1, Ti}, {0, 0, 
  Ni}, {0, -1, Ti}, {0, -2, Ni}, {0, -3, Ti}, {-1, 4, Ni}, {-1, 3, 
  Ti}, {-1, 2, Ni}, {-1, 1, Ti}, {-1, 0, Ni}, {-1, -1, Ti}, {-1, -2, 
  O}, {-2, 5, Ti}, {-2, 4, Ni}, {-2, 3, Ti}, {-2, 2, Ni}, {-2, 1, 
  Ti}, {-2, 0, Ni}, {-2, -1, Ti}, {-3, 6, Ni}, {-3, 5, Ti}, {-3, 4, 
  Ni}, {-3, 3, Ti}, {-3, 2, O}, {-3, 1, Ti}, {-3, 0, Ni}} *)
$\endgroup$
4
  • $\begingroup$ O like the letter O, right (oxygen instead of nickel)? I think that is what I have there $\endgroup$
    – Jason B.
    Commented Feb 22, 2016 at 14:31
  • $\begingroup$ My mistake, the font used confused me. I apologize. $\endgroup$
    – Bob Hanlon
    Commented Feb 22, 2016 at 14:34
  • $\begingroup$ No worries, it's really troublesome for me that the 2 keys are right next to each other, and you'd be surprised how often I type the wrong one lol $\endgroup$
    – Jason B.
    Commented Feb 22, 2016 at 14:35
  • $\begingroup$ @akonar, No problem. Welcome to the site, take the tour to familiarize yourself with it. If this solved your problem, you could mark the post as solved. :-) $\endgroup$
    – Jason B.
    Commented Feb 25, 2016 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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