You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: searchrange.m
    path of file: ./repos/mdtoolbox/mdtoolbox
    
    the code of the file until where you have to start completion: function [pair, dist] = searchrange(a, b, rcut, box)
%% searchrange
% finds all atoms of 'crd1' that are within cutoff 'rcut' distance of 'crd2' atoms
%
%% Syntax
%# pair = searchrange(crd1, crd2, rcut) #Non-PBC
%# [pair, dist] = calcpairlist(crd1, crd2, rcut) #Non-PBC
%# pair = calcpairlist(crd1, crd2, rcut, box) #PBC
%# [pair, dist] = calcpairlist(crd1, crd2, rcut, box) #PBC
%
%% Description
%  This code finds all the atoms in 'crd1' that are within cutoff
%  distance 'rcut' for atoms in 'crd2'. 
%  Atom coordinates should be 3-dimensional points of vector 1 x 3natom.
%  The grid-cell algorithm by Heinz and Hünenberger (JCC, 2004)
%  is used. If the cutoff distance is large compared to the system
%  size, then the exhaustive search algorithm is used instead of
%  the grid-cell. When 'box' is given, periodic boundary condition
%  (PBC) is assumed. 
%
% * crd1 - coordinates in 3-dimensional Cartesian space
%          [1 x 3natom' double]
% * crd2 - coordinates in 3-dimensional Cartesian space
%          [1 x 3natom'' double]
% * rcut - cutoff distance
%          [scalar double]
% * box  - box size. When this is given, PBC is assumed
%          [1 x 3 double]
% * pair - pair list. 1st column is the index of crd2 atom,
%          2nd column is the index of crd1 atom
%          [n x 2 integer]
% * dist - distance of the corresponding pair
%          [n x 1 double]
%
%% Example
%# % calculate Ca atom contact pairlist
%# [pdb, crd] = readpdb('lys.pdb');
%# index_ca = selectname(pdb.name, 'CA');
%# index_ca = find(index_ca);
%# crd = crd(to3(index_ca));
%# crd = decenter(crd);
%# [pair, dist] = searchrange(crd, [0.0 0.0 0.0 3.0 4.0 5.0], 12.0);
%# 
%# % test
%# [pair2, dist2] = searchrange_exhaustive(crd, [0.0 0.0 0.0 3.0 4.0 5.0], 12.0);
%# hold off;
%# s  = sparse(pair(:, 1),  pair(:, 2),  1, 2, 164); spy(s,  'b')
%# hold on;
%# s2 = sparse(pair2(:, 1), pair2(:, 2), 1, 2, 164); spy(s2, 'r')
%# all(find(s) == find(s2))
%# all(abs(sort(dist) - sort(dist2)) < 0.00001)
%
%% See also

    
function [pair, dist] = searchrange(a, b, rcut, box)
%% searchrange
% finds all atoms of 'crd1' that are within cutoff 'rcut' distance of 'crd2' atoms
%
%% Syntax
%# pair = searchrange(crd1, crd2, rcut) #Non-PBC
%# [pair, dist] = calcpairlist(crd1, crd2, rcut) #Non-PBC
%# pair = calcpairlist(crd1, crd2, rcut, box) #PBC
%# [pair, dist] = calcpairlist(crd1, crd2, rcut, box) #PBC
%
%% Description
%  This code finds all the atoms in 'crd1' that are within cutoff
%  distance 'rcut' for atoms in 'crd2'. 
%  Atom coordinates should be 3-dimensional points of vector 1 x 3natom.
%  The grid-cell algorithm by Heinz and Hünenberger (JCC, 2004)
%  is used. If the cutoff distance is large compared to the system
%  size, then the exhaustive search algorithm is used instead of
%  the grid-cell. When 'box' is given, periodic boundary condition
%  (PBC) is assumed. 
%
% * crd1 - coordinates in 3-dimensional Cartesian space
%          [1 x 3natom' double]
% * crd2 - coordinates in 3-dimensional Cartesian space
%          [1 x 3natom'' double]
% * rcut - cutoff distance
%          [scalar double]
% * box  - box size. When this is given, PBC is assumed
%          [1 x 3 double]
% * pair - pair list. 1st column is the index of crd2 atom,
%          2nd column is the index of crd1 atom
%          [n x 2 integer]
% * dist - distance of the corresponding pair
%          [n x 1 double]
%
%% Example
%# % calculate Ca atom contact pairlist
%# [pdb, crd] = readpdb('lys.pdb');
%# index_ca = selectname(pdb.name, 'CA');
%# index_ca = find(index_ca);
%# crd = crd(to3(index_ca));
%# crd = decenter(crd);
%# [pair, dist] = searchrange(crd, [0.0 0.0 0.0 3.0 4.0 5.0], 12.0);
%# 
%# % test
%# [pair2, dist2] = searchrange_exhaustive(crd, [0.0 0.0 0.0 3.0 4.0 5.0], 12.0);
%# hold off;
%# s  = sparse(pair(:, 1),  pair(:, 2),  1, 2, 164); spy(s,  'b')
%# hold on;
%# s2 = sparse(pair2(:, 1), pair2(:, 2), 1, 2, 164); spy(s2, 'r')
%# all(find(s) == find(s2))
%# all(abs(sort(dist) - sort(dist2)) < 0.00001)
%
%% See also
% searchrange_exhaustive
%
%% References
% Heinz, T.N. & Hünenberger, P.H.
% "A fast pairlist-construction algorithm for molecular simulations
% under periodic boundary conditions." 
% J Comput Chem 25, 1474–1486 (2004). 
%
%% setup variables
natom = numel(b)./3;
rcut2 = rcut^2;

%% setup cell
is_pbc = false;

if ~exist('box', 'var') || isempty(box)
  is_pbc = false;

  minx = min([a(1:3:end