MATLAB: Randomly convert exact number of 1 to 0 in binary matrix

binary matrixMATLABonezero

Hi.
I have binary matrix 3×3.
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!

Best Answer

Try
%%

x = [ 0 1 0
1 0 1
1 1 1];
%%
ix_one = find( x == 1 );
ix_set_zero = randi( numel(ix_one), 1,2 );
x( ix_one( ix_set_zero ) ) = 0