我正在尝试创建一个分布,然后根据某个条件我删除一些粒子并保留其他粒子并以行向量形式排列它们。一旦完成过滤,我想通过索引存储左边点的坐标。
我的想法是使用索引来提取满足条件的坐标posx,posy,posz。 我无法做到这一点。以下是代码。任何和所有输入将是有帮助的人。任何更简单的方法都是最有帮助的。 我是Matlab的新手,所以请原谅我天真的问题。 谢谢 。 。
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
答案 0 :(得分:0)
我会试一试,但是您发布的代码中没有定义一些变量。我在我的代码中放置了这些地方的评论。
%Initialize >>npics variable here
%Initialize some vectors and matrices that were not in your code
distances = zeros(1,maxDistances) %maxDistances is a number that represents how many distances you will have to index into this vector
posx = zeros(1,maxPosx); posy = zeros(1,maxPosy); posz = zeros(1,maxPosz)
for n = 1 : 1 : npics
fprintf('%d',n);
%SECTION 1: Creating Distributions
%Declaration of orgin for simulation
mux = 0; muy = 0; muz = 0;
%mean for the normrnd. We set the mean to zero to start from zero.
%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]);
for ii = 1 : 1 : length(l)
for jj = 1 : 1 : length(l)
distances{ii}{jj} = sqrt((x(ii) - x(jj)).^2 + (y(ii) - y(jj)).^2 + (z(ii) - z(jj)).^2);
if distances{ii}{jj} < blockade %blockade is undefined; you must put some `Bool` or `Int` value here, or set its value earlier in your code
distances{ii}{jj} = 0;
elseif distances{ii}{jj} >= blockade
posx(n) = x(jj);
posy(n) = y(jj);
posz(n) = z(jj);
end
end
end
end
我对你的代码所做的只是根据条件,我将符合这些条件的项放入一个空行向量中。这是你想要的,因为你的规格有点不清楚,你的代码缺少一些初始化变量?
另外,为什么你有两个具有相同端点的for
循环?您想获取矩阵的索引值吗?
此外,您错过了sigma
功能的normrnd
参数的初始化。你可以阅读它here on the MathWorks documentation.
答案 1 :(得分:0)
此代码会根据您的“距离”标准删除彼此过于接近的点。没有必要使用“距离”作为单元格数组,因为您没有使用它做任何事情。事实上,如果你不重复使用某些东西,不要存储它们的所有值。我将坐标放在allpoints
中,以便它们始终保持在一起。
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