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: fneighbors2.m
    path of file: ./repos/malini/Codes/FC_EC_calculation code/DynamicFC/jplv7/spatial/weights
    
    the code of the file until where you have to start completion: function nnlist = fneighbors2(d, xcoord, ycoord, nnmax, delorder)
%
% fneighbors2(d, xcoord, ycoord, nnmax, delorder)
%
%This function finds the nnmax nearest neighbors to each observation. Users
%should set mmax to be equal or higher to the number of neighbors they will
%need in the future. 
%
%INPUT:
%
%A weight matrix d, such as coming from Delaunay triangles (e.g., fdelw2).
%This is n by n, and can be symmetric or asymmetric.
%
%The n by 1 locational vectors xcoord, ycoord
%
%The scalar nnmax which is the largest number of neighbors needed in future
%spatial regressions.
%
%The optional scalar delorder equals 1, 2, 3, or 4. This limits the extent to search for neighbors
%to the sum of the delaunay orders. In other words, delorder=2 limits the search to the delaunay
%neighbors and the neighbors of neighbors while delorder=4 limits the search to neighbors, neighbors
%neighbors, neighbors of neighbors of neighbors, and neighbors of neighbors of neighbors of neighbors!
%Lower values of delorder speed computation and reduce memory. The default is 2.
%
%OUTPUT:
%
%The disk file smats.mat which contains a sequence of nnmax sparse matrices each
%containing at most a single element 1 in each row. These are labeled s1,
%s2, s3, .... snnmax. The functions fsym_neighbors2 and fasym_neighbors2
%work with smat and its component files.
%
%NOTES:
%
%One can optimize the likelihood over neighbors and weighting of neighbors
%as in:
%
%Pace, R. Kelley and Ronald Barry, O.W. Gilley, C.F. Sirmans, 
%“A Method for Spatial-temporal Forecasting with an Application to Real Estate Prices,” 
%International Journal of Forecasting, Volume 16, Number 2, April-June 2000, p. 229-246.
%
%Written by Kelley Pace, www.spatial-statistics.com, 6/97 and revised
%12/26/02.

    
function nnlist = fneighbors2(d, xcoord, ycoord, nnmax, delorder)
%
% fneighbors2(d, xcoord, ycoord, nnmax, delorder)
%
%This function finds the nnmax nearest neighbors to each observation. Users
%should set mmax to be equal or higher to the number of neighbors they will
%need in the future. 
%
%INPUT:
%
%A weight matrix d, such as coming from Delaunay triangles (e.g., fdelw2).
%This is n by n, and can be symmetric or asymmetric.
%
%The n by 1 locational vectors xcoord, ycoord
%
%The scalar nnmax which is the largest number of neighbors needed in future
%spatial regressions.
%
%The optional scalar delorder equals 1, 2, 3, or 4. This limits the extent to search for neighbors
%to the sum of the delaunay orders. In other words, delorder=2 limits the search to the delaunay
%neighbors and the neighbors of neighbors while delorder=4 limits the search to neighbors, neighbors
%neighbors, neighbors of neighbors of neighbors, and neighbors of neighbors of neighbors of neighbors!
%Lower values of delorder speed computation and reduce memory. The default is 2.
%
%OUTPUT:
%
%The disk file smats.mat which contains a sequence of nnmax sparse matrices each
%containing at most a single element 1 in each row. These are labeled s1,
%s2, s3, .... snnmax. The functions fsym_neighbors2 and fasym_neighbors2
%work with smat and its component files.
%
%NOTES:
%
%One can optimize the likelihood over neighbors and weighting of neighbors
%as in:
%
%Pace, R. Kelley and Ronald Barry, O.W. Gilley, C.F. Sirmans, 
%“A Method for Spatial-temporal Forecasting with an Application to Real Estate Prices,” 
%International Journal of Forecasting, Volume 16, Number 2, April-June 2000, p. 229-246.
%
%Written by Kelley Pace, www.spatial-statistics.com, 6/97 and revised
%12/26/02.

%obtaining the number of observations.
n=length(xcoord);

if nargin<5
    delorder=2;
end