2017-08-08 24 views
0

私は分布を作成しようとしていますが、特定の条件に基づいていくつかのパーティクルを削除し、他のものを保持して、それらを行ベクトル形式に配置します。フィルタリングが完了したら、インデックスを介して左上の点の座標を保存します。インデックスから座標を抽出する

私の考えは、インデックスを使用して、条件を満たす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 
+1

npics、length(l)とblockageとは何ですか?あまりにも多くの未知の変数があるので、私たちはコードを実行することはできません。あなたは私たちにいくつかの情報をもっと与える必要があります。 – 10a

+0

blockade = 15 * 10^-6%normallndによって作成された2点最小標準距離 sigmax = 10; SLMビームの%1-sigma x sigmay = 10; SLMの%シグマy梁内のビーム sigmaz = 10; um内の青色ビームの%シグマz npics = 2;繰り返し数% パーティクル数= 100; l =セル(粒子数、1); 距離=セル(npics、1); posx = cell(npics、1); posy =セル(npics、1); posz = cell(npics、1); – Tarun

+0

あなたの質問をコード全体で編集してください。編集ボタンはあなたの投稿の左下にあります。 – 10a

答えて

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 
+0

選択基準で大きな助言をいただきありがとうございます。コードのhepに感謝します。 :) – Tarun

+0

コードはあなたが望むように動作していますか? – 10a

+0

ええ、それは:)です。もう一度ありがとう。 – Tarun

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 

私はあなたのコードにしたすべてのは、条件に基づいて、私は空の行ベクトルにこれらの条件を満たしたアイテムを置きました。仕様が少し不明で、コードにいくつかの初期化された変数がないために、これが必要でしたか?

また、同じエンドポイントを持つ2つのループがあるのはなぜですか?行列のインデックス値を取得しますか?

normrnd関数のsigmaパラメータの初期化もありません。あなたはそれについて読むことができますhere on the MathWorks documentation.

+0

こんにちはプログラミング愛好家。あなたの助けに感謝します。私は以前posx、posy、poszを抽出するためにインデックスを使用することを考えていましたが、あなたのメソッドに基づいています。それは可能でなければならない。 matlabのセル構造は、私が正直であると私を困惑させています。 – Tarun

+0

@タルン問題ありません!それ以上の問題がある場合は、私に知らせてください!セルの構造は混乱しています。したがって、私はあなたのようにここで行うような行列インデクシングで 'for'ループを使用しようとします。これが助けられたら、答えを承認してください! – ProgrammingEnthusiast

+1

こんにちはプログラミングマニア。あなたの助けに感謝します。 :) – Tarun

関連する問題