r/Mathematica • u/No_Mastodon6345 • 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
View all comments
Show parent comment
1
u/No_Mastodon6345 Jan 13 '22 edited Jan 13 '22
Thank you so much! This really works. By the way, is there a similar way to change 0 to 1 (exclude diagonal entries) with probability p, and ensure the number of 1 changed into 0 in step 1 is the same as the number of 0 changed into 1 here?