%% Test Program for Dimention Reduction using Fuzzy Logic : 

irisdata; 
X = Data(:, 1:4); 
ldim = size(X, 2); 

covx = cov(X);
[COEFF,latent,explained] = pcacov(covx); 

Y = X*COEFF(:, 1:3); 
hdim = size(Y, 2); 

%%  Generate cluster on X*

XY = [X, Y]; 
c = 5;
% [IDX,C] = kmeans(XY, k); 

[C, IDX] = fcm(XY, c); 
% [mx, id] = max(C, [], 1); 


%% Define Membership Functions for Consequent parameters 

AC = C(:, 1:ldim); 

[V, idx] = sort(AC, 1); 
Lj = min(XY(:, 1:ldim), [], 1); 
Hj = max(XY(:, 1:ldim), [], 1); 

BL = V - [Lj - 0.05*(Hj - Lj); V(1:end-1, :)]; 
BR = [V(2:end, :); Hj + 0.05*(Hj - Lj)] - V; 

[mn, id] = sort(idx, 1);

BL = BL(repmat(0:c:c*(ldim-1), c, 1)  + id); 
BR = BR(repmat(0:c:c*(ldim-1), c, 1)  + id); 

%% Therefore the system has triangular membership functions y = trimf(x,[a b c]); 
%  where the Center is stored on A the left one is in BL and the right one
%  is in BR. 

% A = compute_matrixA(X, AC, BL, BR);







