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: knn_distmat.m
    path of file: ./repos/scGEAToolbox/+run/thirdparty/k-NN-code
    
    the code of the file until where you have to start completion: function knnlength = knn_distmat(D, K)
%KNN K-Nearest Neighbor classifier using an arbitrary distance matrix

    
function knnlength = knn_distmat(D, K)
%KNN K-Nearest Neighbor classifier using an arbitrary distance matrix
%
%  knnlength = knn(D, K)
%
%  Input and output arguments:
%   D     (matrix) of size NxN: This is a precalculated dissimilarity (distance matrix).
% NOTE:  D MUST BE  SYMMETRIC!
%   K    (scalar) the K in K-NN classifier
%   knnlength:   Length of kNN graph
%
% Copyright (c) Huzefa Neemuchwala,( hneemuch@umich.edu ). All rights reserved.

[ND1, ND2] = size(D);
if K >= ND1
    error('First NN is point itself and hence discarded. Hence K < N, strictly')
end