function [coord,sort_coord,permut,prox_permut,vaf,...
    alpha_multiplier,alpha_vaf,alpha_coord] = skew_symmetric_scaling(prox)

% SKEW_SYMMETRIC_SCALING performs a closed-form least squares scaling of a
% skew-symmetric proximity matrix.
%
% syntax: [coord,sort_coord,permut,prox_permut,vaf,...
%   alpha_multiplier,alpha_vaf,alpha_coord] = skew_symmetric_scaling(prox)
%
% COORD contains the least-squares coordinates which are sorted from
% smallest to largest in SORT_COORD; VAF is the variance they account for;
% PERMUT is the object permutation
% corresponding to SORT_COORD with PROX_PERMUT the reordered skew-symmetric
% proximity matrix.  For equally-spaced coordinates, ALPHA_MULTIPLIER
% defines the multiplicative constant on the integer-valued coordinates; a
% collection of equally-spaced coordinates is given by ALPHA_COORD with
% ALPHA_VAF the variance they account for.

n = size(prox,1);

coord = (1/n)*sum(prox);

sort_coord = sort(coord);

permut = zeros(n,1);

for i = 1:n
    test_value = sort_coord(i);
    for j = 1:n
        if (coord(j) == test_value)
            permut(i) = j;
        end
    end
end

prox_permut = prox(permut,permut);

numer = 0.0;
denom = 0.0;

for i = 1:(n-1)
    for j = (i+1):n
        
        numer = numer + (prox(i,j) - (coord(j) - coord(i)))^2;
        
        denom = denom + (prox(i,j))^2;
        
    end
end


vaf = 1 - (numer/denom);   



suma = 0.0;
sumb = 0.0;

for i = 1:(n-1)
    for j = (i+1):n
        suma = suma + prox(permut(i),permut(j))*(j-i);
        sumb = sumb + (j-i)^2;
    end
end

alpha_multiplier = suma/sumb;

numer = 0.0;
denom = 0.0;

for i = 1:(n-1)
    for j = (i+1):n
        
        numer = numer + (prox(permut(i),permut(j)) - alpha_multiplier*(j-i))^2;
        
        denom = denom + (prox(permut(i),permut(j)))^2;
        
    end
end

alpha_vaf = 1 - (numer/denom);

int_aver = (n*(n+1)/2)/n;

alpha_coord = zeros(n,1);

for i = 1:n
    alpha_coord(i) = alpha_multiplier*(i - int_aver);
end