There is a list with numbers 1 to 8 ( p1
) and another list (j
) which contains 4 numbers which shows different 4 positions of the elements in list p1
. I want to take 4 elements from p1
related to the 4 numbers of positions from the list j
and multiply those selected 4 numbers from list p1
by 2.
The code below is a function that do the procedure that I have mentioned above :
p1 = {1, 2, 3, 4, 5, 6, 7, 8};
DoMul[p1_] := Module[{nmu, e1, j, mul, Replacement},
j = {1, 6, 7, 8};
i = 1; While[i <= 4,
e1 = Extract[p1, {j[[i]]}];
mul = 2*e1;
Replacement = ReplacePart[p1, j[[i]] -> mul];
; i++];
Replacement];
The problem is that I do not know how to update p1
in each iteration ( There are 4 iterations ) and the result will be :
{1, 2, 3, 4, 5, 6, 7, 16}
but the correct answer must be :
{2, 2, 3, 4, 5, 12, 14, 16}
Does anyone know how to update the p1
list in each iteration?