How to assign gaussian random numbers on meshgrid?
9 次查看(过去 30 天)
显示 更早的评论
Hi,
I have very simple problem regarding assigning gaussian or uniform random numbers, and zeros on each of the meshgrid points. I tried with following way
nx=6;
ny=6;
[x,y]=meshgrid(1:nx,1:ny);
M=[x(:),y(:);zeros(nx,ny)];
M=[x(:),y(:);normrnd(nx,ny)];
However, it does not work. I am missing the correct syntax. Could anyone help me to rectify it?
0 个评论
回答(2 个)
Star Strider
2017-8-5
Try this:
nx=6;
ny=6;
[x,y]=meshgrid(1:nx,1:ny);
zg = randn(size(x)); % Gaussian Random Numbers
zu = rand(size(x)); % Uniform Random Numbers
figure(1)
surf(x,y,zg)
grid on
title('Gaussian Random Numbers')
figure(2)
surf(x,y,zu)
grid on
title('Uniform Random Numbers')
2 个评论
Star Strider
2017-8-5
The figures are simply to demonstrate the code.
The ‘zg’ and ‘zu’ arrays are the assignments you want.
Walter Roberson
2017-8-5
You have
nx=6;
ny=6;
[x,y]=meshgrid(1:nx,1:ny);
After that, x and y are going to be 6 x 6.
Then you have
M=[x(:),y(:);zeros(nx,ny)];
The x(:) part is going to reshape the 6 x 6 x array into (6*6) x 1 . The y(:) is going to reshape the 6 x 6 y array into (6*6) x 1. The x(:),y(:) is therefore going to be 36 x 2
The zeros(nx,ny) is going to be 6 x 6.
Now, you are using ";" between those parts, so you are trying to put the 6 x 6 array "below" the 36 x 2 array.
If you need an array with x and y and z coordinates for some reason, then I would suggest
M=[x(:), y(:), zeros(nx*ny, 1)];
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!