# Error in Fuzzy c mean code

2 views (last 30 days)
shawin on 26 May 2015
Answered: Walter Roberson on 26 May 2015
Kindly please could you help me with FCM code , the find() function did not work ?
k=3;
e = 0.001;
[N,D] = size(data);
%generate andom number 469 row and three column.
U = rand(N, K);
%=========================================================================%
for i = 1:N
U(i,:) = U(i,:)/(sum(U(i,:)));
end
%=========================================================================%
U_old = zeros(N, K);
Center = zeros(K, D);
n = 0;% counter
m = 1.5;
%=========================================================================%
while (1)
if (norm(U - U_old) < e)
Center
U
n
break;
else
n = n + 1;
for j = 1:K
P = 0;
for i = 1:N
Center(j,:) = Center(j, :) + data(i,:)*(U(i,j)^m);
P = P + (U(i,j)^m);
end
Center(j, :) = Center(j, :) / P;
end
%========================================================================%
U_old = U;
for i = 1:N
for j = 1:K
temp1 = 0;
temp2 = 0;
temp = 0;
for k = 1:K
temp1 = norm(data(i, :) - Center(j, :));
temp2 = norm(data(i, :) - Center(k, :));
temp = temp + (temp1/temp2)^(2/(m-1));
end
U(i,j) = 1/temp;
end
end
end
end
%===========================================================
Shawin Karim 16 hours ago
there is an error in this code which is when i cal for
maxU = max(U);
index1 = find(U(1,:)== maxU);
index2 = find(U(2,:)== maxU);
index3 = find(U(3,:)== maxU);
figure
line(data(index1,1),data(index1,2),'linestyle','none','marker','*','color','g');
line(data(index2,1),data(index2,2),'linestyle','none','marker', 'o','color','r');
line(data(index3,1),data(index3,2),'linestyle','none','marker','v','color','b');
% hold on
% plot(center(1,1),center(1,2),'ko','markersize',15,'LineWidth',2)
% plot(center(2,1),center(2,2),'kx','markersize',15,'LineWidth',2)
% plot(center(3,1),center(3,2),'k*','markersize',15,'LineWidth',2)
the index are empty , and i do not know how to call the center ??

Walter Roberson on 26 May 2015
You have
maxU = max(U)
by default, max() operates along the first non-scalar dimension. Your U is a 2D array so max(U) is going to be the same as max(U,1), finding the per-column maximums.
Then you have
index1 = find(U(1,:)== maxU);
This matches the first row against the per-column maximums, and that comparison would only be true somewhere if something in the first row happened to be the exact same as the per-column maximums for that column. The same for the second and third rows. If you consider a matrix such as
[0 0 0; 0 0 0; 0 0 0; 1 1 1]
then max() of that would be [1 1 1] and you can see that none of those maximum values happen to occur in the first three rows. Your matrix is a lot bigger than 3 rows, so you cannot expect that you will definitely find matches in the first three rows.