load risk_rate.dat

tic;

opts = statset('Maxiter',1000);

best_vaf = 0.0;
store_vaf = zeros(1,1);

for k = 1:1

[coords,stress] = mdscale(risk_rate,2,'Criterion','metricstress', 'Options',opts);

n = size(coords,1);

distance_matrix = zeros(n,n);

for i = 1:n
    for j = 1:n
        
        distance_matrix(i,j) = sqrt(((coords(i,1) - coords(j,1))^2) + ...
            ((coords(i,2) - coords(j,2))^2));
    end
end

ave_diff = (sum(sum(risk_rate - distance_matrix)))/(n*(n-1));

ave_proximity = (sum(sum(risk_rate)))/(n*(n-1));

risk_rate_vec = squareform(risk_rate);

distance_vec = squareform(distance_matrix);

r = corrcoef(risk_rate_vec',distance_vec');
vaf = r(1,2)^2;

store_vaf(k) = vaf;

if(vaf > best_vaf)
    
    best_vaf = vaf;
    best_coords = coords;
    
end
end

store_vaf;

sorted_vafs = sort(store_vaf');

sorted_vafs
best_vaf
best_coords

axis equal

plot(best_coords(:,1),best_coords(:,2),'ko')

hold on

for i = 1:18
    
    objectlabels{i,1} = int2str(i);
    
end

text(best_coords(:,1),best_coords(:,2),objectlabels,'fontsize',10,'verticalalignment','bottom')

toc;
