how to define a 3D volume in a matrix

12 次查看(过去 30 天)
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?

采纳的回答

tlawren
tlawren 2012-6-29
编辑:tlawren 2012-6-29
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.

更多回答(1 个)

Walter Roberson
Walter Roberson 2012-6-29
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.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by