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: scale_dist3_knn.m
    path of file: ./repos/scGEAToolbox/+run/external/mt_SoptSC/symnmf2
    
    the code of the file until where you have to start completion: function A = scale_dist3_knn(D, nn, knn, useSparse)
%SCALE_DIST3_KNN
% A = scale_dist3_knn(D, nn, knn) returns a
% self-tuned affinity matrix A based on the distance
% matrix D. Each observation is only connected to its
% 'knn' neighbors. The affinity values are defined as:
%     A_ii = 0, for all i
%     A_ij = exp(-D_ij / (sigma_i * sigma_j)),
%           if i ~= j and
%           the i-th observation is one of the 'knn'
%           neighbors of the j-th observation
%           or vice versa
%     A_ij = 0, otherwise
% For any i, sigma_i is the Euclidean distance between
% the i-th observation and its nn-th neighbor.

    
function A = scale_dist3_knn(D, nn, knn, useSparse)
%SCALE_DIST3_KNN
% A = scale_dist3_knn(D, nn, knn) returns a
% self-tuned affinity matrix A based on the distance
% matrix D. Each observation is only connected to its
% 'knn' neighbors. The affinity values are defined as:
%     A_ii = 0, for all i
%     A_ij = exp(-D_ij / (sigma_i * sigma_j)),
%           if i ~= j and
%           the i-th observation is one of the 'knn'
%           neighbors of the j-th observation
%           or vice versa
%     A_ij = 0, otherwise
% For any i, sigma_i is the Euclidean distance between
% the i-th observation and its nn-th neighbor.
% The returned affinity matrix A is a sparse matrix.
%
% A = scale_dist3_knn(D, nn, knn, useSparse) returns a sparse
% matrix if useSparse is true, and returns a dense matrix otherwise.
%
% Assumptions on the distance matrix D:
%   When D is a dense matrix:
%     D_ij is the squared Euclidean distance between the
%     i-th and j-th observations.
%   When D is a sparse matrix:
%   (e.g. constructed for image segmentation)
%     If D_ij is nonzero, it is the squared Euclidean
%       distance between the i-th and j-th observations.
%     If D_ij (i~=j) is zero, it means the distance
%       between the i-th and j-th observations is infinity
%       (i.e. the corresponding affinity value is 0).
%     Finally, D_ii=0 (for all i) by definition.
%
% This method was proposed in the following paper:
%     L. Zelnik-Manor, P. Perona,
%     Self-tuning spectral clustering.
%     Advances in Neural Information Processing Systems 17 (NIPS '04), pp. 1601--1608.
% The authors also posted their Matlab code at:
%     http://webee.technion.ac.il/~lihi/Demos/SelfTuningClustering.html
% However, their implementation is different from the
% definition of A in the paper. In particular,
%     A_ij = exp(-D_ij / max((sigma_i*sigma_j), 0.004))
% in their 'scale_dist' function.
%
% Our 'scale_dist3' function here implements the
% original definition of A as stated in the beginning
% of this help document.
%
% This function is used for experiments in the following paper:
%     Da Kuang, Chris Ding, Haesun Park,
%     Symmetric Nonnegative Matrix Factorization for Graph Clustering,
%     The 12th SIAM International Conference on Data Mining (SDM '12), pp. 106--117.
% Please cite this paper if you find this code useful.
%

if (nargin < 4)
    useSparse = true;
end