Generate n samples of random variables based on a given discrete uniform distributions. Can anyone tell me what is the difference between the codes I wrote?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
DDD
el 8 de Sept. de 2020
Comentada: Rena Berman
el 9 de Oct. de 2020
Question: Write a MATLAB program to sample K values from the probability mass function Pi=i/55, i=1, 2, ….10. Plot the histogram of generated values for K=50, 500, and 5000.
Version 1:
Version 2:
I am new to Matlab. I don't know the difference, but both codes get the same results.
Thanks in advance!
2 comentarios
Respuesta aceptada
Walter Roberson
el 8 de Sept. de 2020
The first code generates one new random number each time through the loop, for a total of sampleK random numbers generated out of which sampleK random numbers are used.
The second code generates sampleK random numbers each time through the loop, and uses the i'th of them, ignorning the others, for a total of sampleK * sampleK random numbers generated out of which sampleK random numbers are used.
If you were to move the
u = rand(sampleK,1);
to before the for loop, then you would be making one call that generated sampleK random numbers all at one time, and then used one at a time. That would be more efficient than generating one random number sampleK times.
4 comentarios
Walter Roberson
el 8 de Sept. de 2020
You could consider using randsample() https://www.mathworks.com/help/stats/randsample.html#d120e595179
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!