how to make a function that generate uniformely distributed random matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Muhammad Usman Saleem
el 7 de Mayo de 2015
Comentada: Purushottama Rao
el 8 de Mayo de 2015
Hi every one..
I am going to make a function which takes three input arguments limit,a,b in that order. The function returns an a-by-b matrix of uniformly distributed random integers between 1 and limit inclusive. I am not allowed to use randi, but i can use rand. To make sure that my result is indeed uniformly distributed, test the output of the function by using the built-in function hist, which plots a histogram.
to make such function i am using that codes
function rad=rad1(limit,n,m)
mat=zeros(n,m);
for i=1:n
for j=1:m
a=rand(1,limit);
mat(i,j)=floor(a)
end
end
end
but i getting error every time.Can anybody assist me to make such function...Thanks in advance
0 comentarios
Respuesta aceptada
Thorsten
el 7 de Mayo de 2015
Define your function as
function r = myrandi(limit, a, b)
r = ceil(limit*rand(a,b));
Check with
a= 10; b = 3000; limit = 12;
r = myrandi(limit, a, b);
hist(r(:), 1:limit)
0 comentarios
Más respuestas (3)
Guillaume
el 7 de Mayo de 2015
Editada: Guillaume
el 7 de Mayo de 2015
The syntax of rand is completely different from that of randi. The arguments you pass to rand are just the size of the matrix you want, never the bounds, rand always return floating point numbers between 0 and 1.
Thus to create a matrix of size m x n, of numbers (floating point) between x and y, you'd do:
r = rand(m, n) * (y-x) + x;
You can figure out the rest of your function yourself.
5 comentarios
Guillaume
el 7 de Mayo de 2015
Editada: Guillaume
el 7 de Mayo de 2015
Please comment in the right answer (and use the {} Code button to format code)
As stated several times:
a = limit * rand; %no need for (1, 1)
WILL NOT generate numbers between [1, limit], but between [0, limit]. I've given the correct formula in my original answer. Just replace y by limit and x by 1.
Secondly, if you want uniformly distributed integer, using floor like you did is wrong, since it will round numbers between [0, 1[ to 0, [1, 2[ to 1, etc. up to [limit-1, limit[ rounded to limit-1, but just [limit] to limit. Hence the probability of limit appearing is much lower.
Hopefully from this, you can see were you went wrong. You need to generate the correct range of floating point numbers before rounding.
Purushottama Rao
el 7 de Mayo de 2015
Replace a=rand(1,limit); with a=limit*rand(1,1); in your code
13 comentarios
Purushottama Rao
el 8 de Mayo de 2015
@MUS: You can try to use disp function after the last ietration for varaible 'matt'
Muhammad Usman Saleem
el 7 de Mayo de 2015
2 comentarios
Michael Haderlein
el 8 de Mayo de 2015
No matter where the variable mat is coming from,
rad=mat(i,j);
will return exactly 1 value (as long as i and j are scalar values). If you want the full array, don't use the indices here
rad=mat;
If you want a short, vectorized and correct version, see Thorsten's answer. As this still seems to be homework and the learning effect of simply using a given answer is rather low, my advise would be to go through this fully equivalent function step by step:
function r = myrandi(limit, a, b)
rand_doubles_0_1=rand(a,b);
rand_doubles_0_limit=limit*rand_doubles_0_1;
r=ceil(rand_doubles_0_limit);
Use a limit of 10 and set a,b to maybe 3 or 4 to keep it simple.
Ver también
Categorías
Más información sobre Multivariate Normal Distribution en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!