how to make a function that generate uniformely distributed random matrix
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Thorsten
2015 年 5 月 7 日
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 件のコメント
その他の回答 (3 件)
Guillaume
2015 年 5 月 7 日
編集済み: Guillaume
2015 年 5 月 7 日
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 件のコメント
Guillaume
2015 年 5 月 7 日
編集済み: Guillaume
2015 年 5 月 7 日
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
2015 年 5 月 7 日
Replace a=rand(1,limit); with a=limit*rand(1,1); in your code
13 件のコメント
Purushottama Rao
2015 年 5 月 8 日
@MUS: You can try to use disp function after the last ietration for varaible 'matt'
Muhammad Usman Saleem
2015 年 5 月 7 日
2 件のコメント
Michael Haderlein
2015 年 5 月 8 日
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.
参考
カテゴリ
Help Center および File Exchange で Multivariate Normal Distribution についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!