Question surf() fuction

1 view (last 30 days)
Esteban Suárez
Esteban Suárez on 28 Mar 2017
Edited: Walter Roberson on 27 Mar 2025
I am generationg a revolution solid of a gaussian fuction and when I use the surf() fuction I get the graphic that I need, but I have to get this figure in a matrix NxN with each element of this matrix be a gray value (it will be my height) but I don´t know how do it!
Can you help me?
This is my code:
r = x(1:1024); %0:0.1:30; x(513:1024)
Unrecognized function or variable 'x'.
z = gaussmf(r,[13.32 0]);%exp(-r.^2/(2*(13.32).^2));
theta = 0:pi/20:2*pi;
xx = bsxfun(@times,r',cos(theta));
yy = bsxfun(@times,r',sin(theta));
zz = repmat(z',1,length(theta));
figure()
surf(xx,yy,zz)
axis equal

Answers (1)

AR
AR on 27 Mar 2025
As per my understanding, you want to convert the 3D surface plot into a 2D NxN matrix where each element represents a grayscale value corresponding to the height of the surface.
To do this conversion, you can follow the below steps:
1. Set the resolution for the output Matrix - size N
N = 1024; % Define the resolution of the output matrix
2. Use “meshgrid” to create a grid (xq,yq) which will represent the NxN matrix.
[xq, yq] = meshgrid(linspace(min(xx(:)), max(xx(:)), N), linspace(min(yy(:)), max(yy(:)), N));
3. Use “griddata” to interpolate zz onto 2D grid (xq,yq).
zq = griddata(xx, yy, zz, xq, yq);
4. Normalize the heights to fit into the grayscale image.
zq_normalized = uint8(255 * mat2gray(zq));
5. Display the 2D matrix as a grayscale image.
figure();
imshow(zq_normalized, []);
title('Grayscale Matrix of Revolution Solid');
You can refer the below MathWorks Documentations for more information:

Categories

Find more on Resizing and Reshaping 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!