% iidfield2.m generates, smooths, and graphs a matrix of iid variables gridsize = [40 40]; [x,y] = easygrid([0 10 0 10], gridsize); % [xmin xmax ymin ymax] % Uniformly distributed field Z = rand(gridsize); % Z is a matrix of Uniform(0,1)'s m = 0.5; % the mean for Uniform(0,1) hmin = 0; % horizontal range minimum hmax = 1; % horizontal range maximum % Normally distributed field %Z = randn(gridsize); % Z is a matrix of N(0,1)'s %m = 0.0; % the mean for N(0,1) %hmin = -2; % horizontal range minimum %hmax = 2; % horizontal range maximum h = [[0 .125 0]; [.125 .5 .125]; [0 .125 0]]; % weights for averaging %h = fspecial('gaussian',[5 5],1); % a different filter for averaging for i=1:100, % smooth up to 100 times subplot(3,2,1) mesh(x,y,Z) view(-10,50) xlabel('Horizontal'); ylabel('Vertical'); zlabel('Intensity'); title('Surface plot'); subplot(3,2,2) mesh(x,y,Z) view(80,50) xlabel('Horizontal'); ylabel('Vertical'); zlabel('Intensity'); title('Surface plot'); subplot(3,2,3) contour(x,y,Z,20) title('Contour plot') subplot(3,2,4) pcolor(x,y,Z) shading flat title('Grayscale plot') subplot(3,2,5) hist(reshape(Z,1,prod(gridsize))) V = axis; % get current xmin,xmax,ymin,ymax V(1)=hmin; V(2)=hmax; % set horizontal range axis(V); % re-set axis limits title('Histogram of field values') Z = conv2(Z-m,h,'same')+m; % smooth the values of Z % conv2 uses zeros on the edge, so we % subtract and add back in the mean m drawnow; % display image now, not when the program is done pause % press a key to continue, Ctrl-C to break end