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!


1 Answer 1


Here is an example:

We first create a diagonal matrix with 0 and 1:

n = 4;
mat = DiagonalMatrix[Table[ RandomInteger[{0, 1}], n]]

Then we change the 1's with probability p to zero:

p = 1/2;
Do[mat[[i, i]] = RandomChoice[{p, 1 - p} -> {0, mat[[i, i]]}], {i, n}];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.