Randomly convert exact number of 1 to 0 in binary matrix
Afficher commentaires plus anciens
Hi.
I have binary matrix 3x3.
0 1 0
1 0 1
1 1 1
Let say, it includes six "1" values on random positions. I need to convert two "1" to "0", but randomly.
0 1 0
1 0 0
1 0 1
one of the possible outcomes.
Thank you for your help!
Réponse acceptée
Plus de réponses (1)
Stephen23
le 22 Mai 2020
>> M = [0,1,0;1,0,1;1,1,1]
M =
0 1 0
1 0 1
1 1 1
>> X = find(M);
>> M(X(randperm(numel(X),2))) = 0
M =
0 0 0
1 0 1
1 1 0
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!