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: neighbors.m
    path of file: ./repos/mtex/EBSDAnalysis/@grain2d
    
    the code of the file until where you have to start completion: function pairs = neighbors(grains,varargin)
% returns the Ids of neighboring grains
%
% Syntax
%
%   % neighbouring relationships within grains
%   pairs = neighbors(grains)
%   pairs = neighbors(grains1,grains2)
%
%   % all neighbouring relationships
%   pairs = neighbors(grains,'full')
%

    
function pairs = neighbors(grains,varargin)
% returns the Ids of neighboring grains
%
% Syntax
%
%   % neighbouring relationships within grains
%   pairs = neighbors(grains)
%   pairs = neighbors(grains1,grains2)
%
%   % all neighbouring relationships
%   pairs = neighbors(grains,'full')
%
% Input
%  grains - @grain2d
%
% Output
%  pairs  - index list of size N x 2
%


% extract grainIds for each boundary segment
gbid = grains.boundary.grainId;
gbid(any(gbid == 0,2),:) = [];

% sort columnwise
gbid = sort(gbid,2,'ascend