euclidean distance between data matrix and centroids

2 views (last 30 days)
If i have data matrix (A) 10 × 10 and i calculated the euclidean distance between the matrix A and centroids (tmp1) using k means based on particle swarm optimization. I used particle swarm to choose the optimal centroids then calculate the distance between data matrix and centroids using k means. But when i put the number of cluster k=2 and run the previouse code i obtained on many values to variable tmp1 but i want to 10 ×2 matrix
%Kmeans Cluster Algorithm Based on Particle Optimization Algorithm
clc;clear all;
format long;
tic
sam=load('A.txt');
N=10;
c1=1.2;c2=1.2;
wmax=0.9;wmin=0.4;
M=200;
K=2;
[S D]=size(sam);%s=row
v=rand(N,K*D);
for i=1:N
clmat(i,:)=randperm(S);
end
clmat(clmat>K)=fix(rand*K+1);
fitt=inf*ones(1,N);
fg=inf;
fljg=clmat(1,:);
x=zeros(N,K*D);
pg=x(1,:);
cen=zeros(K,D);
fitt2=fitt;
for t=1:M
for i=1:N
ww = zeros(S,K);
for ii = 1:S
ww(ii,clmat(i,ii)) = 1;
end
ccc=[];tmp=0;
for j = 1:K
sumcs = sum(ww(:,j)*ones(1,D).*sam);
countcs = sum(ww(:,j));
if countcs==0
cen(j,:) =zeros(1,D);
else
cen(j,:) = sumcs/countcs;
end
ccc=[ccc,cen(j,:)];
aa=find(ww(:,j)==1);
if length(aa)~=0
for k=1:length(aa)
tmp=tmp+(sum((sam(aa(k),:)-cen(j,:)).^2));
end
end
end
x(i,:)=ccc;
fitt2(i) = tmp; %Fitness value
end
//
for i=1:N
if fitt2(i)<fitt(i)
fitt(i)=fitt2(i);
y(i,:)=x(i,:);
if fitt2(i)<fg
pg=x(i,:);
fg=fitt2(i);
fljg=clmat(i,:);
end
end
end
bfit(t)=fg;
w = wmax - t*(wmax-wmin)/M;
for i=1:N
v(i,:)=w*v(i,:)+c1*rand(1,K*D).*(y(i,:)-x(i,:))+c2*rand(1,K*D).*(pg-x(i,:));
x(i,:)=x(i,:)+v(i,:);
for k=1:K
cen(k,:)=x((k-1)*D+1:k*D);
end
for j=1:S
tmp1=zeros(1,K);
for k=1:K
tmp1(k)=sum((sam(i,:)-cen(k,:)).^2);%dist
end
[tmp2 clmat(i,j)]=min(tmp1);
end
end
end
  2 Comments
Jan
Jan on 24 Sep 2018
This code is free of comments. Therefore it is nearly unusable and hard to understand or to maintain. Even using a proper indentation would improve the readability (Ctrl-A Ctrl-I).
The clear all is a useless killer: Why removing all loaded function from the memory only to waste time with reloading them from the slow disk? Prefer to use functions to keep your workspace clean.
We cannot run your function due to the missing input files. The description of the problem is not clear yet also: " i obtained on many values to variable tmp1 but i want to 10 ×2 matrix" Can you elaborate this?
muhammad ismat
muhammad ismat on 24 Sep 2018
The input matrix A=
0 1 1 0 1 1 0 1 0 1
1 0 0 1 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 1 0
1 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 1
1 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 1 0 0
1 0 0 0 0 0 1 0 0 0
At line tmp1(k)=sum((sam(i,:)-cen(k,:)).^2) The variable tmp1 must have a matrix 10× 2,but when excuted, this variable takes many values.

Sign in to comment.

Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!