how to define a 3D volume in a matrix

12 ビュー (過去 30 日間)
A
A 2012 年 6 月 29 日
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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by