how to define a 3D volume in a matrix
조회 수: 8 (최근 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?
댓글 수: 0
채택된 답변
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.
댓글 수: 0
추가 답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!