randi generate a normally distributed integer matrix
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I encountered a problem while studying the courses officially provided by matlab. The question requires using the randi function to generate a normally distributed rather than uniformly distributed integer matrix. After consulting the help file of the randi function, I did not find this function.
4 comentarios
Dyuman Joshi
el 25 de Oct. de 2023
You might have misread it as integers rather than numbers.
However, this is only a guess. For a definite feedback, please provide the information @Steven has asked in the comment above.
John D'Errico
el 26 de Oct. de 2023
Editada: John D'Errico
el 26 de Oct. de 2023
Can you use randi to generate a normally distributed matrix? Of course not. You may be mistaken. Or the person writing the question may have not understood what they are asking. After all, people are only human. Even those who write questions as part of courses or textbooks, even teachers.
You could use randi as part of something to generate a sample that will APPROXIMATE a normal distribution, with some effort. I can think of several ways to do that.
If you can show/quote the actual quetion, we might be able to help you. But doing what you seem to think you want to do is not possible.
Respuestas (1)
Walter Roberson
el 26 de Oct. de 2023
The following illustrates that it is possible to create an array of integers whos population statistics approximate normal distribution. The larger the population generated, the more the array (here called discrete) would approximate normal distribution.
This does not show you anything about how to create such a distribution starting with randi() -- just that there are meaningful senses in which it could exist.
rng('shuffle')
N = 1000;
continuous = randn(N,1);
scalefactor = (2^53 - 1);
bins = -1:.1:1;
discrete = round(continuous * scalefactor);
descaled = discrete ./ scalefactor;
sdc = std(continuous)
sdd = std(descaled)
sdi = std(discrete)
r = sdi / sdc
scalefactor / r
histogram(continuous, bins)
histogram(descaled, bins)
histogram(continuous - descaled)
histogram(discrete, bins*scalefactor)
[H,P,CI,STATS] = ttest2(continuous, descaled)
2 comentarios
Bruno Luong
el 26 de Oct. de 2023
Editada: Bruno Luong
el 26 de Oct. de 2023
One could use the well known central limit theorem, no need to mess with number bit coding and poor results.
But that is not the point. It is still not a normal distribution strictly speaking
n=1000000;
ns=100; % larger -> better approximation
r=sum(randi([-1000 1000],ns,n))/(sqrt(sum((-1000:1000).^2)*ns/(2001)));
histogram(r,'Normalization','pdf');
x=linspace(-3,3);
hold on
plot(x,1/sqrt(2*pi)*exp(-x.^2/2),'g','linewidth',2)
Walter Roberson
el 26 de Oct. de 2023
Any finite sampling of normally distribute samples will not be normally distributed "strictly speaking".
Ver también
Categorías
Más información sobre Random Number Generation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!