function [Z, Xcluster, Ycluster, A, cluster] = isodata(X, Y, k, L, I, ON, OC, OS, NO, min);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parametros interos de la funcion %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s=size(X);
s=s(2);
cluster=zeros(1,s); % Espacio temporal interno de la funcion.
iter=0;
final=0;
vuelve3=0;
A=1; % Lo inicializamos para el primer caso.
primeravez=1; % Para en el primer cilo del while no nos pida la modificacion de parametros.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1.Inicializacion de los centros %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z= inicializa_centros(X, Y, A);
%%%%%%%%%%%%%%%%%%%%%%%%
% Programa principal %
%%%%%%%%%%%%%%%%%%%%%%%%
while final==0, %inicio del bucle de iteraciones.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2.Establecer los valores de los parametros %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if primeravez==0,
if vuelve3==0,
[Ltemp, Itemp, ktemp, ONtemp, OCtemp, OStemp]=parametros(L, I, k, ON, OC, OS, iter);
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3.Calcula y asigna a cada coordenada {X,Y} su centro mas cercano %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
primeravez=0;
vuelve3=1;
for i=1:s,
cluster(i)=cercano(X(i), Y(i), Z, A);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4.Eliminar agrupamientos %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funcion que nos reduce (si hace falta) el numero de centros y de agrupamientos encontrados.
[Z, A, cluster]=eliminar(A, cluster, Z, X, Y, ON);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 5.Actualiza los centros de los agrupamientos %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z=recalcula(cluster, X, Y, A, Z);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 6.Terminar, dividir o mezclar %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (iter==I),
final=1;
next=0;
else
next=decide78(iter, k, A);
end;
%%%%%%%%%%%%%%%
% 7.Dividir %
%%%%%%%%%%%%%%%
if next==1,
next=2;
hubo_division=0;
A2=A;
divide=0;
% Dispersion por agrupamiento, dispersion global
% Y dispersion por variable y componente de maxima dispersion por orden respectivo.
[Di, D, STM]= dispersion(X, Y, Z, cluster, A);
% Division / Divide todos los agrupamientos que cumplan la condicion
i=0; % Si se cumplen las condiciones realiza una division e itera.
while (hubo_division==0) & (i < A), % Si para algun...
i=i+1;
index=find(cluster==i); % Index nos indica los valores del agrupamiento a particionar.
sindex=size(index);
sindex=sindex(2);
if (STM(i)>OS) & ( ((Di(i,1)>D(1)) & (Di(i,2)>D(2)) & (sindex>(2*(ON+1)))) | (A<=(k/2)) ),
hubo_division=1;
next=1;
[Z, cluster]=dividir(STM, A, cluster, Z, i, (A+1), X, Y); % Division.
A=A+1; % Indicamos que hay un nuevo agrupamiento.
iter=iter+1;
end;
end;
end;
%%%%%%%%%%%%%%%
% 8.Mezclar %
%%%%%%%%%%%%%%%
if next==2,
%Calcula y ordena las L distancias menores entre los centros.
[orden, Dij]= distancia_centros(A, Z, OC, L); %Si orden==0 --> no hay nada para mezclar.
% Union de agrupamientos.
if orden(1) > 0,
[cluster, Z, A]=union(A, orden, cluster, Z, Dij);
% Recacula los centros.
Z=recalcula(cluster, X, Y, A, Z);
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 9.Terminar o volver a iterar %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if next==2
[iter,final,vuelve3]= termina_o_itera(iter, I, NO);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Final del while(bucle principal del programa) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Elmina puntos que no cumplan al minima distancia %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:s
temp=0;
P=[X(j) Y(j)];
for i=1:A,
if distancia(P,Z(i,:)) > min,
temp=temp+1;
end;
end;
if temp==A,
cluster(j)=0;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Separamos los puntos en k vectores representativos de los k patrones %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xcluster=0;
Ycluster=0;
for m=1:k
inedx=0;
index=find(cluster==m);
s2=size(index);
s2=s2(2);
for n=1:s2
Xcluster(1,n,m)= X(index(n));
Ycluster(1,n,m)= Y(index(n));
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fin del programa principal %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Codigo de las subfunciones utilizadas %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funcion para calcular el centro mas cercano a cada punto %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [m] = cercano(x, y, Z, k)
dtemp=0;
d=0;
for j=1:k
P=[x y];
d=distancia(Z(j,:), P); % Distancia del centro al punto.
if j<2,
m=j; % La primera distancia siempre es valida.
dtemp=d;
elseif d < dtemp,
m=j; % Nos quedamos con el centro al que corresponde
dtemp=d; % la menor de las distancias.
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Funcion para reasignar los centros %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zout1] = recalcula(cluster, X, Y, k, Z) %Realiza la media de los puntos asigandos a cada clase corersp. a los centros
s=size(X);
s=s(2);
valor=zeros(1,k);
Zout=zeros(k,2);
for m=1:k
index=find(cluster==m);
if isempty(index)==0
sindex=size(index);
sindex=sindex(2);
Zout1(m,1)=(sum(X(index))) / sindex;
Zout1(m,2)=(sum(Y(index))) / sindex;
else
Zout1(m,:)=Z(m,:);
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dispersion por agrupamietno y dispersion global %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Ditemp, Dtemp, STMAX] = dispersion(X, Y, Z, cluster, A)
Ditemp=zeros(A,2);
Dtemp=zeros(1,2);
ST=zeros(A,2);
STMAX=zeros(1,A);
for i=1:A,
suma=[0 0];
index=find(cluster==i);
sindex=size(index);
for j=index,
P=[X(j), Y(j)];
评论0