I am trying to create a distribution and then based on a certain condition I remove some particles and keep the others and arrange them in a row vector form. Once, the filtering is done , I want to store the coordinates of the left over points via the indices.
My idea was to use the indices to extract the coordinates posx,posy,posz which satisfy the condition. I am unable to do this. Following is the code. Any and all inputs will be helpful guys. Any simpler method would be most helpful. I am new to Matlab so please forgive my naive question. Thanks . . .
clear all;
%=============Minimum Allowable Distance/Blockade Radius=====================
blockade = 15*10^-6;% blockade radius in um
%=============Sigma of the RED LASER beam from the SLM=====================
sigmax = 10;% 1-sigma x of the SLM beam in um
sigmay = 10;% 1-sigma y of the SLM beam in um
%=============Sigma of the BLUE LASER beam from the SLM====================
sigmaz = 10;% sigma z of the blue beam in um
%==================Number of Scan Steps====================================
npics =500; %number of iterations
%=============Number of initial particles in the excitation volume in the MOT Stage===================
numberofparticles = 100; % Number of points per iteration
%=============Creating a cell system for importing GPT Data into===========
l = cell(numberofparticles,1);
distances = cell(npics,1);
posx = cell(npics,1);
posy = cell(npics,1);
posz = cell(npics,1);
for n=1:1:npics
fprintf(' %d ', n);
%----------------------------------------------------------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 1: Creating Distributions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------------------------------------------
%============Declaration of orgin for simulation===========================
mux = 0;
muy = 0;
muz = 0;
%=============Creating a x,y,z coordinate system for the ion===============
x = normrnd(mux,sigmax*10^-6,[1 numberofparticles]);
y = normrnd(muy,sigmay*10^-6,[1 numberofparticles]);
z = normrnd(muz,sigmaz*10^-6,[1 numberofparticles]);
%%%%%%%%%%%%%METHOD 2%%%%%%%%%%%%%%%%
for i = 1:1:length(l)
for j = 1:1:length(l)
distances{i}{j} = sqrt((x(i) - x(j)).^2 + (y(i) - y(j)).^2 + (z(i) - z(j)).^2);
if distances{i}{j} < blockade
distances{i}{j} = 0;
end
if distances{i}{j} >= blockade
posx{j} = x(j);
posy{j} = y(j);
posz{j} = z(j);
end
end
end
end
This code deletes the points that lie too close to each other according to your "distance" criterion. There was no need to use "distance" as a cell array because you were not doing anything with it. In fact, if you don't re-use something, don't store all their values. I put the coordinates in allpoints
so that they are always kept together.
clear all;
%=============Minimum Allowable Distance/Blockade Radius=====================
blockade = 15*10^-6;% blockade radius in um
%=============Sigma of the RED LASER beam from the SLM=====================
sigmax = 10;% 1-sigma x of the SLM beam in um
sigmay = 10;% 1-sigma y of the SLM beam in um
%=============Sigma of the BLUE LASER beam from the SLM====================
sigmaz = 10;% sigma z of the blue beam in um
%==================Number of Scan Steps====================================
npics =2; %number of iterations
%=============Number of initial particles in the excitation volume in the MOT Stage===================
numberofparticles = 100; % Number of points per iteration
allpoints = cell(npics,1);
for n=1:npics
fprintf(' %d ', n);
%----------------------------------------------------------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 1: Creating Distributions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------------------------------------------
%============Declaration of orgin for simulation===========================
mux = 0;
muy = 0;
muz = 0;
%=============Creating a x,y,z coordinate system for the ion===============
x = normrnd(mux,sigmax*10^-6,[1 numberofparticles]);
y = normrnd(muy,sigmay*10^-6,[1 numberofparticles]);
z = normrnd(muz,sigmaz*10^-6,[1 numberofparticles]);
figure(n)
plot(x,y,'r*')
hold on
allpoints = cell(npics,1);
for i = 1:length(x)
allpoints{i} = [x(i) y(i) z(i)]; % Store all coordinates in 1 cell array
end
%%%%%%%%%%%%%METHOD 2%%%%%%%%%%%%%%%%
% Because allpoints will change size (some points are removed if "distance" is smaller than blockade, we cannot use a for-loop i=1:length(allpoints) because then the max value of i is already fixed while allpoints will only get smaller. Therefore, i will get larger than the eventual length of allpoints
i = 1;
j = 1;
while true % As long as i is not larger than the length of allpoints, not all points have been evaluated with each other
j = i+1;
while true
coordi = allpoints{i};
coordj = allpoints{j};
distances = sqrt((coordi(1) - coordj(1)).^2 + (coordi(2) - coordj(2)).^2 + (coordi(3) - coordj(3)).^2);
if distances < blockade
allpoints(j) = []; % Using the round brackets, the cell is deleted
% When a point is removed from the list, j does not need to be increased because the next point that needs to be evaluated comes at the place of the old point, so at position j and not j+1
else
j = j + 1; % Increase j to evaluate the next point.
end
if j>length(allpoints)
break;
end
end
i = i + 1;
if i>= length(allpoints)
break;
end
end
allpoints = cell2mat(allpoints);
plot(allpoints(:,1),allpoints(:,2),'bo')
end