
%This is a simple example of unsupervised learning from a set of 24 images,
%each of which is a noisy version of a "| or a "-" or a "+". The algorithm "learns" this
clear,randn('state',4);M=[0 0 0;1 1 1;0 0 0];O=[0 1 0;0 1 0;0 1 0];P=M+O;sigma=0.2;
p=P(:);m=M(:);o=O(:);%Unwrap 3X3 images to 9X1
A=[p*ones(1,8) m*ones(1,8) o*ones(1,8)];%A=training matrix.
N=sigma*randn(9,24);A=A+N;%Add noise to training matrix.
%reorder columns of training matrix A at random:
A=A(:,[17 21 24 12 8 15 9 3 16 20 7 11 14 2 18 5 19 13 6 4 10 23 1 22]);
%These are the 24 training images:
figure,for I=1:24;subplot(4,6,I),imagesc(reshape(A(:,I),3,3)),colormap(gray),axis off,end;
%Depict the training matrix as an image (too many numbers otherwise).
%Compute SVD of training matrix and stem-plot the singular values.
[U S V]=svd(A);
figure,subplot(3,1,1),imagesc(A),colormap(gray),title('Depiction of training matrix as an image.')
subplot(3,1,3),stem(diag(S)),title('Singular values of training matrix. Only 2 are significant.')
%Plot columns of training matrix A as coordinates in subspace of 2 largest singular vectors:
W=U(:,1:2);w=W'*A;figure,plot(w(1,:),w(2,:),'+'),title('Training images depicted in 2-D space. Clearly 3 clusters.')
%Clearly the 24 training images cluster into 3 regions.
X=W*[2 1 1;0 1 -1];%{(2,0),(1,1),(1,-1)}
PHAT=reshape(X(:,1),3,3);MHAT=reshape(X(:,2),3,3);OHAT=reshape(X(:,3),3,3);
figure,subplot(3,3,1),imagesc(PHAT),colormap(gray),title('Estimated "+" image.')
subplot(3,3,2),imagesc(MHAT),colormap(gray),title('Estimated "-" image.')
subplot(3,3,3),imagesc(OHAT),colormap(gray),title('Estimated "|" image.')

