function [p,W,EX,EV,ET]=LearnSLMPA(X,c,V,cV,T,cT);

%
% Alternative learning procedure for dendrite computation in a single layer
% morphological perceptrons applyed for classification problems.
%
% M. E. Valle, November 18, 2005
%
% This code comes with no guarantee or warranty of any kind.
%
% function [p,W,EX,EV,ET]=LearnSLMPA(X,c,V,cV,T,cT);
%
% Input: X = Training matrix. Each column is a point, i.e. X=[X1,...Xp].
%        c = Training class vector. Each component stands for the
%            corresponding class in a one-class problem, i.e 
%            c=[c1,c2,...,cp] where ci is the class of Xi.
%        V = Validation matrix.
%       cV = Validation class vector.
%        T = Test matrix.
%       cT = Test class vector.
%
% Output:  p = Response of the dendrits.
%          W = Sinaptic weight matrix.
%         EX = Vector containing the error of the training set.
%         EV = Vector containing the error of the validation set.
%         ET = Vector containing the error of the test set.
%
% Requires SLMP.m
%

tol=1.e-4;

[N M]=size(X);

mu=mean(X,2);
sigma=std(X,0,2);
 
if nargin>2
    MV=length(cV);
end
if nargin>4
    MT=length(cT);
end 

k=1;
c1=find(c);
W(:,k,1)=-min(X(:,c1),[],2);
W(:,k,2)=-max(X(:,c1),[],2);
p(1)=1;
y=SLMP(p,W,X);

if nargin>2
    yV=SLMP(p,W,V);
    EV(k)=norm(yV-cV,1)/MV;
end
if nargin>4
    yT=SLMP(p,W,T);
    ET(k)=norm(yT-cT,1)/MT;
end

EX(k)=norm(y-c,1)/M;
misc=find(abs(y-c).*[1:M]);

while length(misc)&(k<200)
    k=k+1;
    p(k)=-1;
    W(:,k,1)=inf*ones(N,1);
    W(:,k,2)=-inf*ones(N,1);
    D=c1;
    gamma=randperm(length(misc));
    gamma=misc(gamma(1));
    
    while length(D)
        mu=inf;
        for i=1:length(D);
            normachess=norm(X(:,gamma)-X(:,D(i)),inf);
            if normachess<mu
                mu=normachess;
                xi=D(i);
            end
        end
        ind=[];
        for i=1:N
            if abs(X(i,gamma)-X(i,xi))==mu
                ind=[ind,i];
            end
        end
        for i=1:length(ind)
            if X(ind(i),xi)<X(ind(i),gamma)
                W(ind(i),k,1)=-(X(ind(i),xi)/2+X(ind(i),gamma)/2);
            end
            if X(ind(i),xi)>X(ind(i),gamma)
                W(ind(i),k,2)=-(X(ind(i),xi)/2+X(ind(i),gamma)/2);
            end
            Dl=[];
            for j=1:length(D)
                if (-W(ind(i),k,1)<X(ind(i),D(j)))&(X(ind(i),D(j))<-W(ind(i),k,2))
                    Dl=[Dl,D(j)];
                end
            end
        end
        D=Dl;
    end
    y=SLMP(p,W,X);
    EX(k)=norm(y-c,1)/M;
    misc=find(abs(y-c).*[1:M]);
    
    if nargin>2
         yV=SLMP(p,W,V);
         EV(k)=norm(yV-cV,1)/MV;
    	   if nargin>4
        		yT=SLMP(p,W,T);
        		ET(k)=norm(yT-cT,1)/MT;
            disp(sprintf('Dendrite Number: %2d, Training Error: %0.4f, Validation Error: %0.4f, Test Error: %0.4f',k,EX(k),EV(k),ET(k)));
         else
            disp(sprintf('Dendrite Number: %2d, Training Error: %0.4f, Validation Error: %0.4f',k,EX(k),EV(k)));
         end
    else
        disp(sprintf('Dendrite Number: %2d, Training Error: %0.4f',k,EX(k)));
    end
end