3
$\begingroup$

Probably very basic, sorry, but I've searched extensively without finding a solution:

list_2 = ReplacePart[list_1, _?EvenQ -> "Sin(value of the entry in this position in list_1)" ]

That is, creating list_2, for every second entry replace its value with the Sinus of the corresponding value in list_1.

So for example {1, pi/2} should return {1, 1}. What is the code I should use in "..."?

On a general note: How does one address the value of the "current position" in a list? Thanks.

$\endgroup$
5
$\begingroup$
list1 = {1, Pi/2, Pi, 3 Pi/2, 3, 10};

list2 = ReplacePart[list1, part_?EvenQ :> foo[list1[[part]]]]

{1, foo[π/2], π, foo[(3 π)/2], 3, foo[10]}

list3 = ReplacePart[list1, part_?EvenQ :> Sin[list1[[part]]]]

{1, 1, π, -1, 3, Sin[10]}

$\endgroup$

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.