从索引中提取坐标
Extracting coordinates from indices
我正在尝试创建一个分布,然后根据特定条件删除一些粒子并保留其他粒子并将它们排列成行向量形式。一旦过滤完成,我想通过索引存储剩余点的坐标。
我的想法是利用索引提取满足条件的坐标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
我会尝试一下,但是您有一些变量没有在您发布的代码中定义。我在代码中的那些地方添加了注释。
%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
循环具有相同的端点?您要获取矩阵的索引值吗?
此外,您还缺少 normrnd
函数的 sigma
参数的初始化。你可以阅读它 here on the MathWorks documentation.
此代码根据您的 "distance" 标准删除彼此距离太近的点。没有必要使用 "distance" 作为元胞数组,因为你没有对它做任何事情。事实上,如果你不重复使用某些东西,就不要存储它们的所有值。我将坐标放在 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
我正在尝试创建一个分布,然后根据特定条件删除一些粒子并保留其他粒子并将它们排列成行向量形式。一旦过滤完成,我想通过索引存储剩余点的坐标。
我的想法是利用索引提取满足条件的坐标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
我会尝试一下,但是您有一些变量没有在您发布的代码中定义。我在代码中的那些地方添加了注释。
%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
循环具有相同的端点?您要获取矩阵的索引值吗?
此外,您还缺少 normrnd
函数的 sigma
参数的初始化。你可以阅读它 here on the MathWorks documentation.
此代码根据您的 "distance" 标准删除彼此距离太近的点。没有必要使用 "distance" 作为元胞数组,因为你没有对它做任何事情。事实上,如果你不重复使用某些东西,就不要存储它们的所有值。我将坐标放在 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