4
$\begingroup$

I have two lists, the first list has a large numerical data, and the other list has the position of elements of the first list that are zero. For example

s = {1, 5, 7, 8, 11 ,10, 3}
Zeroelement = {1, 3, 5}

I have tried use ReplacePart[s2, Zeroelement -> 0.0], but does not work.

I would like to replace the elements 1, 3, and 5 in "s" by 0 without using a loop. That's is possible?,

The lists have different lengths.

$\endgroup$
4
$\begingroup$

it was very easy, simply do s[[Zeroelement]]=0

$\endgroup$
4
$\begingroup$

It can be done with ReplacePart like this:

s = {1, 5, 7, 8, 11, 10, 3};
z = {1, 3, 5};
ReplacePart[s, Thread[{z}] -> 0]

{0, 5, 0, 8, 0, 10, 3}

$\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.