How to assign gaussian random numbers on meshgrid?
7 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Star Strider
on 5 Aug 2017
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 Comments
Star Strider
on 5 Aug 2017
The figures are simply to demonstrate the code.
The ‘zg’ and ‘zu’ arrays are the assignments you want.
Walter Roberson
on 5 Aug 2017
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)];
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!