Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
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];

Antworten (2)

Sarah Crimi
Sarah Crimi am 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 Kommentar
Guillaume
Guillaume am 7 Feb. 2019
Bearbeitet: Guillaume am 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 am 7 Feb. 2019
Bearbeitet: Kevin Phung am 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);

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by