First of all I´m new to Mathematica; I´m trying to exchange distinct values of a list with values of another list (piecewise). I thought ReplacePart
in combination with Position
will do it, but it doesn´t yield the wanted result.
Here is an example:
original data:
data = Table[RandomInteger[]*100, {x, 0, 20}, {y, 0, 20}, {z, 0, 20}];
next get the position:
posSelect = Position[data, a_ /; a == 100, {3}, Heads -> False];
generate 2nd list:
lengthSelect = Length[Extract[data, posSelect]];
newVal = RandomInteger[1, lengthSelect];
updated list
data2 = data
data2 = ReplacePart[data, posSelect -> newVal];
data2 now contains at each selected position the entire elements of newVal. What is wrong with this approach? How to correctly exchange these selected values with the values of the 2nd list?
posSelect = Position[data, a_ /; a == 100, {3}, Heads -> False];
you just writeposSelect = Position[data, 100, {3}, Heads -> False];
$\endgroup$posSelect
is not a list of integers but rather a list of lists of integers, so you'll have toMap
your way around this one. $\endgroup$