%%% This code is to demonstrate how visualizing data can help see patterns
%%% in your data

%%%% first lets generate some random data (3 random patterns of length 20)
mean1=rand(1,20);
mean2=rand(1,20);
mean3=rand(1,20);

%%%  rr will be used to pick which pattern vector we draw from
%%%  for our test data
rr= rand(20,1)

%%% rr is now used to generate from the index patterns  [0 0 1]  [0 1 0] [1 0 0]  
indmat= [rr<.33  rr>=.33&rr<.67 rr>.67];
meanmat= [mean1; mean2; mean3] 


%%%  The next line is a very nice efficient way of creating our test data from
%%%  the samples of our means
AA = indmat*meanmat;
rows=[1:20]

%%% Now we  add noise to our test data to make the problem interesting
AA=AA+.3*rand(20);

%% displaying numbers is not very helpful -- Can you see the pattern?
%% (several rows are copies of each other plus noise)
AA
pause

%% displaying as an image is nicer
imagesc(AA)
pause

%% sorting first can reveal patterns 
T=clustergram(AA, 'Colormap', jet, 'Rowlabels', rows)
pause


[Idx, C, SUMD, D] = kmeans(AA, 3) 

imagesc([AA(Idx==1,:); AA(Idx==2,:); AA(Idx==3,:)])
pause

