How to assign gaussian random numbers on meshgrid?

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

Risposte (2)

Star Strider
Star Strider il 5 Ago 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 Commenti
Sudipta Sinha
Sudipta Sinha il 5 Ago 2017
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 il 5 Ago 2017
The figures are simply to demonstrate the code.
The ‘zg’ and ‘zu’ arrays are the assignments you want.

Accedi per commentare.


Walter Roberson
Walter Roberson il 5 Ago 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)];
  1 Commento
Sudipta Sinha
Sudipta Sinha il 5 Ago 2017
Modificato: Sudipta Sinha il 5 Ago 2017
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.

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by