sorter.m

来自「matlab的数学物理方程数值算法源程序。这是"Numerical Method」· M 代码 · 共 41 行

M
41
字号
function sD = sorter(x,L,sD)
% sorter - Function to sort particles into cells
% sD = sorter(x,L,sD)
% Inputs
%    x       Positions of particles
%    L       System size
%    sD      Structure containing sorting lists
% Output
%    sD      Structure containing sorting lists

%* Find the cell address for each particle
npart = sD.npart;
ncell = sD.ncell;
jx = floor(x*ncell/L) + 1;
jx = min( jx, ncell*ones(npart,1) );

%* Count the number of particles in each cell
sD.cell_n = zeros(ncell,1);
for ipart=1:npart
  sD.cell_n( jx(ipart) ) = sD.cell_n( jx(ipart) ) + 1;
end

%* Build index list as cumulative sum of the 
%  number of particles in each cell
m=1;
for jcell=1:ncell
  sD.index(jcell) = m;
  m = m + sD.cell_n(jcell);
end

%* Build cross-reference list
temp = zeros(ncell,1);     % Temporary array
for ipart=1:npart
  jcell = jx(ipart);       % Cell address of ipart
  k = sD.index(jcell) + temp(jcell);
  sD.Xref(k) = ipart;
  temp(jcell) = temp(jcell) + 1;
end

return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?