how to define a 3D volume in a matrix
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
tlawren
am 29 Jun. 2012
Bearbeitet: tlawren
am 29 Jun. 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.
0 Kommentare
Weitere Antworten (1)
Walter Roberson
am 29 Jun. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!