function [PP_Index]=GENE_TRIP2_RMVES(X,N,p,N_max)     %   we can change index
% p-norm purest pixel identification

X_ori=X;
load CnCn ;
Rw=CNN;Rw_ori=Rw;
% _______________

[M,L] = size(X); 
d = mean(X,2);
U = X-d*ones(1,L);
R = U*U';
Rx_true = (R./L) - Rw;
[eV D] = eig(Rx_true);
C = eV(:,end:-1:M-(N_max-2));
load CCCC;
load dddd;
Xd_t = C'*(X-d*ones(1,L));  % Dimension reduced data vectors (Xd is (N-1)-by-L)

p=2; 

%===algorithm=========
A_set=[]; Xd = [Xd_t; ones(1,L)]; index = [];                          % N is endmember, p is p-norm, Xd is N by L
[val ind]=max(sum(abs(Xd).^p).^(1/p));   
A_set = [A_set Xd(:,ind)];
index = [index ind];                                        % save index
for i=2:N    
    XX = (eye(N_max) - A_set * pinv(A_set) ) * Xd; 
    [val ind]=max(sum(abs(XX).^p).^(1/p));                               %    N*p'(p-norm)*N*
    A_set = [A_set Xd(:,ind)];                             % find A_est one by one
    index = [index ind];                                        % save index
end
% ALPHA = Xd_t(:,index);
%A_est=(C*Xd_t(:,index)+d*ones(1,N));
PP_Index=index;
 %time = etime(clock,t0);
