Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

How do I find the probability that 0 would be selected if a random number was selected?

1 visualizzazione (ultimi 30 giorni)
d = [1, 0.5, 0.01, 0.5, 0, 1, 2, 15.31, 33, 12, 3, 7, 2, 15, 38, 0, ...
2.5, 0, 0.4, 17.8, 0.5, 10.6, 0, 0.1, 11, 12, 1, 0.8, 0, 7, 10, ...
0, 10.1, 4.14, 0.5, 14.7, 0, 0, 0, 0, 0.1, 0. 37, 3.1, 25, 0, 1, ...
1.4, 7.5, 17.9, 30, 0, 0, 0, 14.2, 30, 23.7, 0, 0, 0, 7.7, 3.4, 0, ...
0, 0, 0, 0, 0, 0, 0.5, 0, 11, 0, 10, 0, 0, 70, 0, 15, 3.5, 9];

Risposte (2)

Sarah Crimi
Sarah Crimi il 7 Feb 2019
d = [1, 0.5, 0.01, 0.5, 0, 1, 2, 15.31, 33, 12, 3, 7, 2, 15, 38, 0, ...
2.5, 0, 0.4, 17.8, 0.5, 10.6, 0, 0.1, 11, 12, 1, 0.8, 0, 7, 10, ...
0, 10.1, 4.14, 0.5, 14.7, 0, 0, 0, 0, 0.1, 0. 37, 3.1, 25, 0, 1, ...
1.4, 7.5, 17.9, 30, 0, 0, 0, 14.2, 30, 23.7, 0, 0, 0, 7.7, 3.4, 0, ...
0, 0, 0, 0, 0, 0, 0.5, 0, 11, 0, 10, 0, 0, 70, 0, 15, 3.5, 9];
%create a counter to count the number of zeros
counter=0;
%Create a loop to add 1 each time there is another 0.
for i=1:length(d)
if(d(i)==0)
counter = counter+1;
end
end
  1 Commento
Guillaume
Guillaume il 7 Feb 2019
Modificato: Guillaume il 7 Feb 2019
Please learn to format your post. I've done it for you this time.
Your code can be summed up to:
counter = nnz(d == 0); %or even: nnz(~d)
A lot simpler!

Kevin Phung
Kevin Phung il 7 Feb 2019
Modificato: Kevin Phung il 7 Feb 2019
Just do:
d = [1, 0.5, 0.01, 0.5, 0, 1, 2, 15.31, 33, 12, 3, 7, 2, 15, 38, 0, ...
2.5, 0, 0.4, 17.8, 0.5, 10.6, 0, 0.1, 11, 12, 1, 0.8, 0, 7, 10, ...
0, 10.1, 4.14, 0.5, 14.7, 0, 0, 0, 0, 0.1, 0. 37, 3.1, 25, 0, 1, ...
1.4, 7.5, 17.9, 30, 0, 0, 0, 14.2, 30, 23.7, 0, 0, 0, 7.7, 3.4, 0, ...
0, 0, 0, 0, 0, 0, 0.5, 0, 11, 0, 10, 0, 0, 70, 0, 15, 3.5, 9];
Probability = sum(d==0)/ numel(d);

Questa domanda è chiusa.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by