how to randomly delete number
1 view (last 30 days)
Show older comments
Hi, i create a 1x3 matrix by using :
M=[0 0 0];
m=round((3-1)*rand(1)+1);
M(m)=1;
and i want to know how i can randomly delete one of the 2 zeros. I thought about starting with
for pos=find(M==0)
end
but i dont know what to do next. Im a beginner. Thanks
0 Comments
Accepted Answer
Rik
on 20 Oct 2018
Edited: Rik
on 20 Oct 2018
Your code looks like you want to randomly fill one location with a one, but it has a bias toward the middle. The code below does not.
M=zeros(1,3);
m=randi(numel(M));%pick a random integer between 1 and the number of elements in M
M(m)=1;
%generate the list of positions with a zero
ind_zero=1:numel(M);ind_zero(m)=[];
%randomly pick 1 element from that list
remove_ind=ind_zero(randperm(numel(ind_zero),1));
%remove the element
M(remove_ind)=[];
1 Comment
Rik
on 23 Oct 2018
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!