Cheking my code related to create 3D cube pourous material
4 次查看(过去 30 天)
显示 更早的评论
This code is designed to generate a 3D volume output, but I am interested in incorporating a specific level of porosity for further research. I'm curious if this modification will serve my intended purpose.
"% Paramètres
dimension = 100; % Taille de l'image 3D
porosity = 0.2; % Porosité (e.g., 20%)
minPoreRadius = 2;
maxPoreRadius = 5;
% Création d'une matrice 3D initialement remplie de zéros
materialMatrix = zeros(dimension, dimension, dimension);
% Génération aléatoire des pores
numPores = round(porosity * dimension^3);
positionsX = randi([1, dimension], 1, numPores);
positionsY = randi([1, dimension], 1, numPores);
positionsZ = randi([1, dimension], 1, numPores);
poreRadii = randi([minPoreRadius, maxPoreRadius], 1, numPores);
% Ajout des pores à la matrice
for i = 1:numPores
pore = strel('sphere', poreRadii(i));
materialMatrix(positionsX(i), positionsY(i), positionsZ(i)) = 1;
materialMatrix = imdilate(materialMatrix, pore);
end
% Affichage de la matrice 3D
figure;
[x, y, z] = meshgrid(1:dimension, 1:dimension, 1:dimension);
scatter3(x(:), y(:), z(:), 5, 'filled', 'MarkerFaceColor', 'r'); % Afficher tous les points
hold on;
[px, py, pz] = ind2sub(size(materialMatrix), find(materialMatrix));
scatter3(px, py, pz, 50, 'filled', 'MarkerFaceColor', 'b'); % Afficher les pores en bleu
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Milieu de matériau poreux 3D');
grid on;
axis equal tight;
view(3);
hold off;
"
0 个评论
回答(1 个)
Aastha
2024-9-24
I tried to reproduce the given code, and I have a few suggestions that can improve the code and create 3D cube porous material. Here are a few of them:
1. Relative Pore Radii:
You have set fixed values for the pore radii. This approach works well if the pore radii are always smaller than the matrix dimension, but it could lead to problems if the pore radii exceed the matrix dimension. To make the code more flexible, set the pore radii relative to the matrix size. The code snippet below illustrates this:
dimension = 20; % Size of the 3D matrix
porosity = 0.02; % Porosity (e.g., 2%)
minPoreRadius = 0.05 * dimension; % Relative to the matrix size
maxPoreRadius = 0.1 * dimension; % Relative to the matrix size
2. Avoiding Complete Matrix Dilation:
When you use “imdilate” function to create pores, the entire “materialMatrix” can become filled with ones by the end of the loop, which is not ideal for visualization. Instead, you can store the positions of the pores separately and then visualize those. This approach avoids cluttering the matrix and provides a clearer visualization. You can achieve this with the following code snippet:
p = [];
for i = 1:numPores
pore = strel('sphere', poreRadii(i));
materialMatrix(positionsX(i), positionsY(i), positionsZ(i)) = 1;
materialMatrix = imdilate(materialMatrix, pore);
[cpx, cpy, cpz] = ind2sub(size(materialMatrix), find(materialMatrix));
cp = [cpx, cpy, cpz];
p = [p; cp]; % Store the pore locations
p = unique(p, 'rows'); % Remove duplicate points
materialMatrix = zeros(dimension, dimension, dimension); % Reset the matrix
end
The above modifications to the code will help you create a 3D cube porous material.
For more information on “imdilate” function, you may refer to the MathWorks documentation mentioned below:
I hope this is helpful!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!