how to make a function that generate uniformely distributed random matrix

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Thorsten
Thorsten il 7 Mag 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)

Più risposte (3)

Guillaume
Guillaume il 7 Mag 2015
Modificato: Guillaume il 7 Mag 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 Commenti
Guillaume
Guillaume il 7 Mag 2015
Modificato: Guillaume il 7 Mag 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.

Accedi per commentare.


Purushottama Rao
Purushottama Rao il 7 Mag 2015
Replace a=rand(1,limit); with a=limit*rand(1,1); in your code
  13 Commenti
Muhammad Usman Saleem
Muhammad Usman Saleem il 7 Mag 2015
@Purushottama Rao when i put semi colon against floor(a) i am getting in output ans a variable which is a vector of length 1. I want to show complete matrix in output..
Purushottama Rao
Purushottama Rao il 8 Mag 2015
@MUS: You can try to use disp function after the last ietration for varaible 'matt'

Accedi per commentare.


Muhammad Usman Saleem
Muhammad Usman Saleem il 7 Mag 2015
@Michael Haderlein thanks for contributions... yes i want to print complete matrix but to my knowledge mat(i,j) at last is getting values from the mat(i,j)=floor(a) which is inside the second for loop. mat(i,j) in the second loop has created when the loops completed. then its complier goes to mat(i,j) at the end to show in the output. Kindly correct me if i am wrong. I want to print all matrix in output.Thanks in advance for assistance..
  2 Commenti
Michael Haderlein
Michael Haderlein il 8 Mag 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.
Muhammad Usman Saleem
Muhammad Usman Saleem il 8 Mag 2015
thank you so much for assistance.... i am getting my error in previous codes.... @Michael Haderlein ....if you like my effort vote up my question..thanks

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by