Azzera filtri
Azzera filtri

Create sequence of images starting from a 3D matrix

4 visualizzazioni (ultimi 30 giorni)
Michele Vanini
Michele Vanini il 23 Apr 2021
Risposto: Vatsal il 17 Mag 2024
Dear all,
I have a system defined by a 21000x3 matrix (av_fin in the attached script) in which every row define the coordinate of an atom position. The first, second and third columns of each row refer to the x , y, and z coordinate, respectively.
I would like to divide my system in a series of slices along the z direction and get a 2D black and white figure of every slice depicting the position of the atoms.
Is there some function that I can use to do this easily?

Risposte (1)

Vatsal
Vatsal il 17 Mag 2024
Hi,
To visualize the distribution of atoms in the system by dividing it into a series of slices along the z-axis and creating a 2D black and white figure for each slice, following approach can be utilized. Assuming you have a 3D matrix "av_fin" prepared, where each row indicates an atom's position in 3D space with columns for x, y, and z coordinates, start by determining the range and desired number of slices along the z-axis. Then, iterate through each slice to identify the atoms it contains. For every slice, plot the 2D positions of these atoms.
Here is an approach for the same:
% Assuming av_fin is your 21000x3 matrix
z_min = min(av_fin(:,3)); % Minimum z-coordinate
z_max = max(av_fin(:,3)); % Maximum z-coordinate
% Define the number of slices or the thickness of each slice
num_slices = 30; % For example, 30 slices
slice_thickness = (z_max - z_min) / num_slices;
% Create a folder to save figures if it doesn't exist
folderName = 'AtomSlices';
if ~exist(folderName, 'dir')
mkdir(folderName)
end
for slice = 1:num_slices
z_start = z_min + (slice - 1) * slice_thickness;
z_end = z_start + slice_thickness;
% Find atoms in the current slice
in_slice = av_fin(:,3) >= z_start & av_fin(:,3) < z_end;
atoms_in_slice = av_fin(in_slice, 1:2); % Get x and y coordinates
% Plot the atoms in the current slice
figure;
scatter(atoms_in_slice(:,1), atoms_in_slice(:,2), 'k.'); % 'k.' for black dots
axis equal; % Keep aspect ratio of x and y the same
xlim([min(av_fin(:,1)), max(av_fin(:,1))]);
ylim([min(av_fin(:,2)), max(av_fin(:,2))]);
title(sprintf('Slice %d', slice));
xlabel('X');
ylabel('Y');
% Save the figure
saveas(gcf, fullfile(folderName, sprintf('Slice_%d.png', slice)));
close; % Close the figure window
end
I hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by