Question surf() fuction

조회 수: 2 (최근 30일)
Esteban Suárez
Esteban Suárez 2017년 3월 28일
편집: Walter Roberson 2025년 3월 27일
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

답변 (1개)

AR
AR 2025년 3월 27일
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:

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by