function [M,COV,INDEX,COL,P1]=kmeansforspeech(DATA,n,N)
subplot(2,2,1)
plot3(DATA(1,:),DATA(2,:),DATA(3,:),'r*')
s1=size(DATA,1);
s2=size(DATA,2)/n;
r=0;
M=[];
for i=1:1:n
M{i}=mean(DATA(:,(i-1)*s2+1:1:(i-1)*s2+s2)')';
end
COL=[];
for iteration=1:1:N
clear M1    
for k=1:1:n
    C{k}=[];
    INDEX{k}=[];
end
count=0;
k=1;
M1=[];
for i=1:1:n
    if(length(M{i})==size(DATA,1))
count=count+1;        
M1{k}=M{i};
k=k+1;
    end
end
n=count;
clear M
M=M1;
S=0;
for i=1:1:size(DATA,2)
    D=[];
   for j=1:1:n
    D=[D sum((DATA(:,i)-M{j}).^2)];
   end
[P,Q]=min(D);
C{Q}=[C{Q} DATA(:,i)];
INDEX{Q}=[INDEX{Q} i];
S=S+P;
end
COL=[COL S];
for i=1:1:n
M{i}=mean(C{i}')';    
end
while(iteration==1)    
subplot(2,2,2)
plot3(DATA(1,INDEX{1}),DATA(2,INDEX{1}),DATA(3,INDEX{1}),'r*')
hold on
plot3(DATA(1,INDEX{2}),DATA(2,INDEX{2}),DATA(3,INDEX{2}),'go')
plot3(DATA(1,INDEX{3}),DATA(2,INDEX{3}),DATA(3,INDEX{3}),'bs')        
plot3(DATA(1,INDEX{4}),DATA(2,INDEX{4}),DATA(3,INDEX{4}),'c<')        
iteration=0;
end
while(iteration==2)
subplot(2,2,3)
plot3(DATA(1,INDEX{1}),DATA(2,INDEX{1}),DATA(3,INDEX{1}),'r*')
hold on
plot3(DATA(1,INDEX{2}),DATA(2,INDEX{2}),DATA(3,INDEX{2}),'go')
plot3(DATA(1,INDEX{3}),DATA(2,INDEX{3}),DATA(3,INDEX{3}),'bs')        
plot3(DATA(1,INDEX{4}),DATA(2,INDEX{4}),DATA(3,INDEX{4}),'c<')        
iteration=0;
end
while(iteration==200)
subplot(2,2,4)
plot3(DATA(1,INDEX{1}),DATA(2,INDEX{1}),DATA(3,INDEX{1}),'r*')
hold on
plot3(DATA(1,INDEX{2}),DATA(2,INDEX{2}),DATA(3,INDEX{2}),'go')
plot3(DATA(1,INDEX{3}),DATA(2,INDEX{3}),DATA(3,INDEX{3}),'bs')        
plot3(DATA(1,INDEX{4}),DATA(2,INDEX{4}),DATA(3,INDEX{4}),'c<')        
iteration=0;
end
end

for i=1:1:n
    COV{i}=cov(C{i}');
end
for i=1:1:n
P1(i)=length(INDEX{i})/size(DATA,2);
end
