如何实现分配算法

时间:2011-12-27 16:56:19

标签: matlab

给定向量y(d x 1)和矩阵Z(d x n),我想为下面的算法实现最优的matlab代码。

Initialization:  
Let z* be the nearest point from y in Z.  
A={z*} (A is the list of assignment for y)  
Z = Z \ {z*} (z* point is removed from matrix Z)  

do  
Let p denotes the center of gravity of A:  
Let z* be the nearest center from y in Z(updated Z)  
Let q denotes the center of gravity of A U {z*}  
if distance(y,q) < distance(y,p) then   
 A = A U {z*} and Z = Z \ {z*}  
else  
 NOP  
endif   
while a new assignment is performed

return A(list of assignments)

function A = Assign(x,Z) 
%
%   x - 1 x N
%   Z - k x N
%
OriginalZ = Z;
[~,index]=sort(pdist2(x,Z));
A = index(1);
Z(A,:)=inf;
loop =1;

while(loop)
  loop =0;
  p=mean(OriginalZ(A,:),1);
  [~,index]=sort(pdist2(x,Z));
  Ad = [A index(1)];
  q = mean(OriginalZ(Ad,:),1);
  if pdist([x;q]) < pdist([x;p])
     A = Ad; Z(Ad,:)=inf;
     loop = 1;
  end
end
end

0 个答案:

没有答案