Matlab 如何实现赋值算法
给定向量y(dx1)和矩阵Z(dxn),我想为下面的算法实现最佳的matlab代码Matlab 如何实现赋值算法,matlab,Matlab,给定向量y(dx1)和矩阵Z(dxn),我想为下面的算法实现最佳的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: L
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)
初始化:
设z*为z中距y最近的点。
A={z*}(A是y的赋值列表)
Z=Z\{Z*}(Z*点从矩阵Z中删除)
做
设p表示A的重心:
设z*为z中距y最近的中心(更新的z)
设q表示U{z*}的重心
如果距离(y,q)<距离(y,p),则
A=A U{z*}和z=z\{z*}
其他的
不
恩迪夫
在执行新任务时
返回(作业列表)
函数A=Assign(x,Z)
%
%x-1xn
%Z-k×N
%
原始Z=Z;
[~,索引]=sort(pdist2(x,Z));
A=指数(1);
Z(A,:)=inf;
循环=1;
while(循环)
循环=0;
p=平均值(原始值z(A,:),1);
[~,索引]=sort(pdist2(x,Z));
Ad=[A指数(1)];
q=平均值(原始z(Ad,:),1);
如果pdist([x;q])
到目前为止您有什么经验?我在实现do..while循环时遇到困难。直到现在初始化,我发现点Y和Z之间的距离,并排序计算距离找到Z*,然后我用INF替换Z的相应列(因为我不想从现在开始考虑这一点)。我觉得这不是实现这个算法的最佳方式。因此,我将其发布在这里。使用示例代码编辑了我的问题。您可以从矩阵中删除它们,而不是将不需要的Z列设置为inf:Z(:,idx)=[]
。也Z(Ad,:)=inf代码中的code>将用inf而不是其列替换Z
的行。请在标题和问题中指定您试图解决的问题,以便其他人将来能够找到它。
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