[datamatrix_cb,datamatrix_eu,corr_cb_eu,gamma_cb_eu,datavectors,coord] = ...
    data_generate_cityblock_and_euclidean(20,0.0,1.0);

datamatrix_cb

datamatrix_eu 

corr_cb_eu 

gamma_cb_eu

coord

tic;

n = 20;

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


for i = 1:100
    
    [outpermone,outpermtwo,coordone,coordtwo,fitone,fittwo,addconone,...
        addcontwo,vaf,monprox] = bimonscalqa(datamatrix_cb,targlin(n),targlin(n), ...
        randperm(n),randperm(n),3,1);
    
    store_vaf(i) = vaf;
    
 
  
    if (vaf > best_vaf)
        
        best_vaf = vaf;
        best_outpermone = outpermone;
        best_outpermtwo = outpermtwo;
        best_coordone = coordone;
        best_coordtwo = coordtwo;
        best_fitone = fitone;
        best_fittwo = fittwo;
        best_addconone = addconone;
        best_addcontwo = addcontwo;
        best_prox = monprox;
        
    end
    
end

sorted_vafs = sort(store_vaf');

sorted_vafs

        best_vaf
        best_outpermone 
        best_outpermtwo
        best_coordone
        best_coordtwo
        
        best_addconone
        best_addcontwo 
        best_prox
        
        axes = zeros(n,2);
        
        for i = 1:n
            
            axes(best_outpermone(i),1) = best_coordone(i);
            axes(best_outpermtwo(i),2) = best_coordtwo(i);
        end
        
        figure(2)
        
        plot(axes(1:n,1),axes(1:n,2),'ko')
        
        hold on
        
        for i = 1:n
            
            objectlabels{i,1} = int2str(i);
            
        end
        
        text(axes(1:n,1),axes(1:n,2),objectlabels,'fontsize',10,'verticalalignment','bottom')
        
        
toc;

cityblock_coordinates = axes;



tic;

opts = statset('Maxiter',1000);

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

for k = 1:100

[coords,stress,disparities] = mdscale(datamatrix_cb,2,'Criterion','stress', 'Start',...
    'random','Replicates',1,'Options',opts);

n = size(coords,1);

distance_matrix = zeros(n,n);
trans_datamatrix_cb = disparities;

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(trans_datamatrix_cb - distance_matrix)))/(n*(n-1));

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

datamatrix_cb_vec = squareform(trans_datamatrix_cb);

distance_vec = squareform(distance_matrix);

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

store_vaf(k) = vaf;

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

store_vaf;

sorted_vafs = sort(store_vaf');

sorted_vafs
best_vaf
best_coords
best_prox

figure(3)

axis equal

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

hold on

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

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

toc;

euclidean_coordinates = [best_coords(:,1),best_coords(:,2)];

[d,z,transform] = procrustes(cityblock_coordinates,euclidean_coordinates);

figure(4)

plot(cityblock_coordinates(:,1),cityblock_coordinates(:,2),'rx',...
    euclidean_coordinates(:,1),euclidean_coordinates(:,2),'b.',...
    z(:,1),z(:,2),'go')

hold on

text(cityblock_coordinates(:,1),cityblock_coordinates(:,2),objectlabels,'fontsize',8,'verticalalignment','bottom')

text(z(:,1),z(:,2),objectlabels,'fontsize',8,'verticalalignment','bottom')

transform(1).b

transform(1).T

transform(1).c

%------------------------------------


tic;

n = 20;

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

for i = 1:100
    
    [outpermone,outpermtwo,coordone,coordtwo,fitone,fittwo,addconone,...
        addcontwo,vaf,monprox] = bimonscalqa(datamatrix_eu,targlin(n),targlin(n), ...
        randperm(n),randperm(n),3,1);
    
    store_vaf(i) = vaf;
    
    if (vaf > best_vaf)
        
        best_vaf = vaf;
        best_outpermone = outpermone;
        best_outpermtwo = outpermtwo;
        best_coordone = coordone;
        best_coordtwo = coordtwo;
        best_fitone = fitone;
        best_fittwo = fittwo;
        best_addconone = addconone;
        best_addcontwo = addcontwo;
        best_prox = monprox;
        
    end
    
end

sorted_vafs = sort(store_vaf');

sorted_vafs

        best_vaf
        best_outpermone 
        best_outpermtwo
        best_coordone
        best_coordtwo
        
        best_addconone
        best_addcontwo 
        best_prox
        
        axes = zeros(n,2);
        
        for i = 1:n
            
            axes(best_outpermone(i),1) = best_coordone(i);
            axes(best_outpermtwo(i),2) = best_coordtwo(i);
        end
        
        figure(5)
        
        plot(axes(1:n,1),axes(1:n,2),'ko')
        
        hold on
        
        for i = 1:n
            
            objectlabels{i,1} = int2str(i);
            
        end
        
        text(axes(1:n,1),axes(1:n,2),objectlabels,'fontsize',10,'verticalalignment','bottom')
        
        
toc;

cityblock_coordinates = axes;



tic;

opts = statset('Maxiter',1000);

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

for k = 1:100

[coords,stress,disparities] = mdscale(datamatrix_eu,2,'Criterion','stress', 'Start',...
    'random','Replicates',1,'Options',opts);

n = size(coords,1);

distance_matrix = zeros(n,n);
trans_datamatrix_eu = disparities;

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(trans_datamatrix_eu - distance_matrix)))/(n*(n-1));

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

datamatrix_eu_vec = squareform(trans_datamatrix_eu);

distance_vec = squareform(distance_matrix);

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

store_vaf(k) = vaf;

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

store_vaf;

sorted_vafs = sort(store_vaf');

sorted_vafs
best_vaf
best_coords
best_prox

figure(6)

axis equal

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

hold on

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

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

toc;

euclidean_coordinates = [best_coords(:,1),best_coords(:,2)];

[d,z,transform] = procrustes(cityblock_coordinates,euclidean_coordinates);

figure(7)

plot(cityblock_coordinates(:,1),cityblock_coordinates(:,2),'rx',...
    euclidean_coordinates(:,1),euclidean_coordinates(:,2),'b.',...
    z(:,1),z(:,2),'go')

hold on

text(cityblock_coordinates(:,1),cityblock_coordinates(:,2),objectlabels,'fontsize',8,'verticalalignment','bottom')

text(z(:,1),z(:,2),objectlabels,'fontsize',8,'verticalalignment','bottom')

transform(1).b

transform(1).T

transform(1).c



