a = random('Normal',1,256,256,256);
b = random('Normal',1,256,256,256);
c = random('Normal',1,256,256,256);
d = random('Normal',1,256,256,256);
e = random('Normal',1,256,256,256);
f = random('Normal',1,256,256,256);
g = random('Normal',1,256,256,256);
h = random('Normal',1,256,256,256);

figure;

a = mat2gray(a);
b = mat2gray(b);
c = mat2gray(c);
d = mat2gray(d);
e = mat2gray(e);
f = mat2gray(f);
g = mat2gray(g);
h = mat2gray(h);

a = im2uint8(a);
b = im2uint8(b);
c = im2uint8(c); 
d = im2uint8(d);
e = im2uint8(e);
f = im2uint8(f);
g = im2uint8(g);
h = im2uint8(h);

c = c * 1.6;
d = d * 1.6;

e = e * .6;
f = f * .6;

g( find(g > 128) ) = g( find(g > 128) ) * 1.6;
g( find(g <= 128) ) = g( find(g <= 128) ) * .6;

h( find(h > 128) ) = h( find(h > 128) ) * 1.6;
h( find(h <= 128) ) = h( find(h <= 128) ) * .6;

%Compare Start - a
v1 = imhist(a);
distance = [];

v2 = imhist(a);
distance(8) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(b);
distance(1) = norm(v1 - v2);

i = 1;

best = min(distance);

while i < 9
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - b
v1 = imhist(b);
distance = [];

v2 = imhist(a);
distance(8) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(b);
distance(1) = norm(v1 - v2);

i = 9;

best = min(distance);

while i < 17
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - c
v1 = imhist(c);
distance = [];

v2 = imhist(a);
distance(8) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(b);
distance(1) = norm(v1 - v2);

i = 17;

best = min(distance);

while i < 25
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - d
v1 = imhist(d);
distance = [];

v2 = imhist(a);
distance(8) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(b);
distance(1) = norm(v1 - v2);

i = 25;

best = min(distance);

while i < 33
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - e
v1 = imhist(e);
distance = [];

v2 = imhist(b);
distance(1) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(a);
distance(8) = norm(v1 - v2);

i = 33;

best = min(distance);

while i < 41
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - f
v1 = imhist(f);
distance = [];

v2 = imhist(b);
distance(1) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(a);
distance(8) = norm(v1 - v2);

i = 41;

best = min(distance);

while i < 49
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - g
v1 = imhist(g);
distance = [];

v2 = imhist(b);
distance(1) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(a);
distance(8) = norm(v1 - v2);

i = 49;

best = min(distance);

while i < 57
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end

%Compare Start - h
v1 = imhist(h);
distance = [];

v2 = imhist(b);
distance(1) = norm(v1 - v2);

v2 = imhist(c);
distance(2) = norm(v1 - v2);

v2 = imhist(d);
distance(3) = norm(v1 - v2);

v2 = imhist(e);
distance(4) = norm(v1 - v2);

v2 = imhist(f);
distance(5) = norm(v1 - v2);

v2 = imhist(g);
distance(6) = norm(v1 - v2);

v2 = imhist(h);
distance(7) = norm(v1 - v2);

v2 = imhist(a);
distance(8) = norm(v1 - v2);

i = 57;

best = min(distance);

while i < 65
    
    best = find( distance == (best));
    subplot(8, 8, i);
    
    if (best == 1)
       imshow(b); 
    elseif (best == 2)
       imshow(c);         
    elseif ( best == 3) 
        imshow(d);
    elseif (best == 4)
        imshow(e);
    elseif (best == 5) 
       imshow(f);
    elseif (best == 6)
       imshow(g);
    elseif (best == 7)
       imshow(h);
    elseif (best == 8) 
       imshow(a);
    end
    
    distance(best) = 9999999999;
    best = min(distance);
    
    i = i + 1;
    
end
