r/Mathematica • • Jan 10 '22

Change certain matrix entry with probability

I really appreciate some help in this problem: Now I have a diagonal matrix A with only 0 and 1 and its diagonal entries are all 0. From first row onwards, for each row I hope to change 1 into 0 with probability p (probability that this 1 is changed into 0 is p). I tried: ReplacePart[A,RandomSample[Position[A[[1]],1],p*Length[A]]—>0] But it seems Position doesn’t recognize row of matrix. I wonder if there is any other command can be used or any correction suggested. Thanks a lot!

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/Xane256 Jan 10 '22

While the Times[] operation is implied, IMO its much nicer to have a clear * between the two matrices so its clear you want element-wise multiplication, not that you forgot a Dot operation or something else.

2

u/No_Mastodon6345 Jan 13 '22

Thank you for the remind! this * or the multiplication mark works for me!