how to define a 3D volume in a matrix

8 visualizaciones (últimos 30 días)
A
A el 29 de Jun. de 2012
I need to define a cylinder(diameter=20cm and height=25cm) in a 3D matrix A(40x40x50) such that each element represents a 5mm x 5mm x 5mm cube. Cube A(i,j,k)=1 if it falls within the cylinder and A(i,j,k)=0 if it falls outside the cylinder. What's the best way to populate this matrix?

Respuesta aceptada

tlawren
tlawren el 29 de Jun. de 2012
Editada: tlawren el 29 de Jun. de 2012
A somewhat sloppy, yet direct way could be ...
% Set dimensions
ds = 5; % mm
xlim = ds*40/2;
ylim = xlim;
zlim = ds*50/2;
% Forms a 41x41x51 matrix
[X,Y,Z] = meshgrid(-xlim:ds:xlim,-ylim:ds:ylim,-zlim:ds:zlim);
% Convert to cm
X = X./10;
Y = Y./10;
Z = Z./10;
% A matrix (matrix of radii)
A = sqrt(X.^2 + Y.^2);
% Set center axis to 1
A(A == 0) = 1;
% Anything outside of cylinder radius (10 cm) set to 0
A(abs(A) > 10) = 0;
% Convert all non-zeros to 1 (all pts on and inside cylinder)
A(abs(A) > 0) = 1;
Here, A is not 40x40x50, but instead 41x41x51. The odd size allows for a center center-axis.

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de Jun. de 2012
If you are aligning the cylinder with a primary axes, then you can create a filled circle and copy that circle along the axes (e.g., repmat)
You do have to be a bit careful about what you mean by "if it falls within the cylinder", when you examine the edge cubes. Even a cube whose center falls within the radius does not necessarily have half of its volume inside the cylinder.

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by