function [P2 T2 A] = PR(P,T,th) % Pattern Reduction if nargin < 3 th = .25; end % R = 10000000; % repeating ID calculation parameter [N na] = size(P); n = round(N * th); [T ind] = sort(T); P = P(ind,:); Q = [T(1) nan nan nan nan nan nan nan nan nan]; q = 1; class{20} = []; c(20) = 0; cc(20) = 0; temp = T; for i = 1:inf ff = (1:N)'.*(temp == Q(q)); f = ff(ff>0); c(q) = length(f); cc(q) = cc(q) + c(q); class{q} = [class{q};f]; temp(f) = NaN; if sum(isnan(temp))==N break end q = q + 1; Q(q) = temp(cc(q-1)+1); cc(q) = cc(q-1) + cc(q); end Q = Q(1:q); a = [1 (cc(1:(q-1))+1)]; A(n,2) = 0; % Matrix showing patterns to be discarded D(N,N) = 0; % Distance matrix of dataset ad(q) = 0; % Average distance of class for i = 1:q d = dmatrix3(P(class{i},:)); ad(i) = sum(sum(d))/(c(i)*(c(i)-1)); D(a(i):cc(i),a(i):cc(i)) = d / ad(i); end [z Z] = IDND5(P,T); DD(q,N) = 0; % Minimum distance for i = 1:q [M ind] = max(Z(class{i})); A(i,1) = ind + a(i) - 1; A(i,2) = i; Z(A(i,1)) = 0; DD(i,:) = D(A(i,1),:); end v(q,N) = 0; for i = (q+1):n % p = round(100*i/n); % if mod(i,R) == 0 % clc,disp(p) % temp = A((A(:,1)>0),1); % P1 = P; T1 = T; % P1(temp,:) = rm(inf,length(temp),na); % [z Z] = IDND5(P1,T1); % Z(temp) = 0; % end for j = 1:q v(j,:) = DD(j,:).*Z; end [M r c] = mmax(v); % r = class, c = pattern Z(c) = 0; A(i,:) = [c r]; DD(r,:) = min(DD(r,:),D(A(i,1),:)); end % p = 100;clc,disp(p) P(A(:,1),:) = []; T(A(:,1)) = []; P2 = P; T2 = T;