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?

回答(2 个)

Star Strider
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 个评论
Sudipta Sinha
Sudipta Sinha 2017-8-5
Thanks for your response. But my problem is different.I need to assign a random number or a constant number at each of the location of the meshgrid, M. For example
M(1,1)=rand1 M(1,2)=rand2 .... like that
I don't need to draw any figure with the data.
Star Strider
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
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)];
  1 个评论
Sudipta Sinha
Sudipta Sinha 2017-8-5
编辑:Sudipta Sinha 2017-8-5
No, it does not work. I need very simple thing. For each location of a grid point, I want to assign zero or random numbers. For example, if I wish to assign a value of a function (say, f=xy ) on each of the grid points, I need perform a very simple operation like f=x.*y. The dimension of the matrix, f, is defined by the dimension of x and y. However, how to pass the x and y information for generating correct dimension of zero and random numbers on each of the grid points.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by