I created an 3D sinusoidal surface. And want to add a normally distributed random noise of zero mean and 0.03mm standard deviation. But it said error on the line with*. Subscripted assignment dimension mismatch. could someone help me check it please
6 views (last 30 days)
Show older comments
%3D sinusoidal surface dx=0.001; %spacing in x in mm dy=0.001; %number of points along x and y nx=128; ny=128; %n=8000; %generate array of x & y x=(0:1:nx-1)*dx; y=(0:1:ny-1)*dy; for j=1:ny for i=1:nx z(j,i)=2*sin(2*pi*x(i)/0.064)+normrnd(0,0.03*ones(nx,1)); end end z=z-mean(mean(z));%shift to 0 mean mesh(x,y,z)
0 Comments
Answers (2)
Youssef Khmou
on 19 Aug 2013
Edited: Youssef Khmou
on 19 Aug 2013
Jianxiang,
As you use x,y loops , at each iteration the CPU computes only one scalar while you used the function normrnd of size 128*1, you can adjust your method :
%3D sinusoidal surface
dx=0.001; %spacing in x in mm
dy=0.001; %number of
%points along x and y
nx=128; ny=128; %
n=8000; %generate array of x & y
x=(0:1:nx-1)*dx;
y=(0:1:ny-1)*dy;
for j=1:ny
for i=1:nx
z(j,i)=2*sin(2*pi*x(i)/0.064)+normrnd(0,0.03);
end
end
z=z-mean(mean(z));%shift to 0 mean mesh(x,y,z)
Or you can simply add the Gaussian noise like the following :
Z=z+normrnd(0,0.03,nx,ny);
0 Comments
See Also
Categories
Find more on Random Number Generation 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!