function [ output_args ] = ImageEigenvectorsMedium( myvec)
%demonstration of "machine learning" following some ideas from 
% "Data Driven Modeling & Scientific Computation", by J.N. Kutz
  %% provide your own letter in 'myvec': a matrix of dimensions similar to c,t,o,d below
 function rletter = randomize(letter)
    dim1 = size(letter,1);
    dim2 = size(letter,2);
    dim = dim1 * dim2;
    rletter = zeros(10,dim);
    for k=1:10
        cra = letter.*(1+rand(size(letter)));
        cra = reshape(cra,1,dim);
        cra(randi(dim))=0;
        cra(randi(dim))=1e-1;
        rletter(k,:)=cra;
        spy(reshape(rletter(k,:),dim1,dim2));pause(0.05);
    end
    end
 function show(letter,dim1,dim2)
    spy(reshape(letter,dim1,dim2));pause(0.05);
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    clf;
%% letter C
    c =[ 1,1,1,0;1,0,0,0;1,0,0,0;1,0,0,0;1,1,1,0];
    cv = reshape(c,1,5*4);
    cr = randomize(c);
    pause;
    o =[ 1,1,1,1;1,0,0,1;1,0,0,1;1,0,0,1;1,1,1,1];
    ov = reshape(o,1,5*4);
    or = randomize(o);
    pause; 
    t =[ 1,1,1,1;0,1,0,0;0,1,0,0;0,1,0,0;0,1,0,0];
    tv = reshape(t,1,5*4);
    tr = randomize(t);
    pause; 
    d =[ 1,1,0,0;1,0,0,1;1,0,0,1;1,0,0,0;1,1,0,0];
    dv = reshape(d,1,5*4);
    dr = randomize(d);
    pause; 
    
    %%%% now find the Eigenvectors of the data set
    crave = sum(cr(1:end,:))/10; show(crave,5,4);title('Average C');pause
    orave = sum(or(1:end,:))/10; show(orave,5,4);title('Average O');pause;
    trave = sum(tr(1:end,:))/10; show(trave,5,4);title('Average T');pause;
    drave = sum(dr(1:end,:))/10; show(drave,5,4);title('Average D');pause;
    
  
    %% now use this data as training images
    
    DATA = [cr;or;tr;dr]; 
    CMAT =  DATA'*DATA;   %% or use C = cov(DATA); %% or use SVD on DATA 
    [V,D] = eigs(CMAT,5,'lm');
   
    %plot(diag(D));pause
   
    figure(1);proj = cv*V; bar(proj);title('C signature');ylim([-5,5]);
    pause;
    figure(2);proj = ov*V; bar(proj);title('O signature');ylim([-5,5]);
    pause;
    figure(3);proj = tv*V; bar(proj);title('T signature');ylim([-5,5]);
    pause;
    figure(4);proj = dv*V; bar(proj);title('D signature');ylim([-5,5]);
    pause;
    myvec = reshape(myvec,1,5*4);
    figure(5); proj = myvec*V; bar(proj);title('Your signature');ylim([-5,5]);pause;
    %%%% 
    
    %% Project: create training images with the letters of your first name
    %% test it with the letter 'X' or 'Z' or anything else. Be creative!
end

