Search code examples
arraysjuliaprobabilitysubstitution

Julia, conditionally substitution of individual elements


I asked a question about this earlier but may be over-complicating the issue. Given an array:

a = [3,4,3,6,3];

what code do I need to execute the following:

if the individual element of a == 3
then with a probability (p=0.67), change the value to 9
else leave the element as a 3

Thx. J


Solution

  • I guess this would be the shortest way to achieve what you want:

    a[findall(x -> x==3 && rand()<=0.67, a)] .= 9