我在matlab中有一组n维代表向量。我必须根据接近度将一组训练向量中的向量分组到由代表向量表示的组。我该怎么做?
2 回答
You can use dsearchn
to find which representative is closest to each point. I would recommend trying the version that doesn't involve a triangulation matrix first. If the memory or CPU performance isn't good enough, look into the triangulation stuff.
如果 n 维向量是指 n 维点的有序列表(这是我对您想要的内容的理解),那么我过去使用平均最近距离完成了此操作。基本上对于向量 1 上的每个点,找到到向量 2 上的点的最小距离。两个向量之间的距离就是所有这些距离的平均值。然而,这不是对称的,因此您应该对向量 2 上的每个点执行相同的过程,找到到向量 1 的最小距离,然后将这两个平均值聚合为最小值、最大值或平均值等...
这是我使用循环制作的一些代码(用于 3d 向量):
function mcd = MCD(fiber1, fiber2, option)
%
%remove NaNs
fiber1(find(isnan(fiber1),1):length(fiber1),:) = [];
fiber2(find(isnan(fiber2),1):length(fiber2),:) = [];
dist = 0;
for k = 1:length(fiber1)
D = [];
for j = 1:length(fiber2)
D = [D distance(fiber1(k,:),fiber2(j,:))];
end;
dist = dist + min(D);
end;
mcd = dist / length(fiber1);
if nargin > 2
dist = 0;
for k = 1:length(fiber2)
D = [];
for j = 1:length(fiber1)
D = [D distance(fiber2(k,:),fiber1(j,:))];
end;
dist = dist + min(D);
end;
mcd2 = dist / length(fiber2);
if strcmp(option,'mean')
mcd = mean([mcd mcd2]);
elseif strcmp(option,'min')
mcd = min([mcd mcd2]);
end;
end;
但这对我来说太慢了。所以这是一个非常快的矢量化(但难以理解)版本:
function mcd = MCD(fiber1, fiber2, option, sampling)
%MCD(fiber1, fiber2)
%MCD(fiber1, fiber2, option)
%MCD(fiber1, fiber2, option, sampling)
%remove NaNs
fiber1(find(isnan(fiber1),1):length(fiber1),:) = [];
fiber2(find(isnan(fiber2),1):length(fiber2),:) = [];
%sample the fibers for speed. Each fiber is represented by "sampling"
%number of points.
if nargin == 4
freq = round(length(fiber1)/sampling);
fiber1 = fiber1(1:freq:length(fiber1),:);
freq = round(length(fiber2)/sampling);
fiber2 = fiber2(1:freq:length(fiber2),:);
end;
%reshape to optimize the use of distance() for speed
FIBER2 = reshape(fiber2',[1,3,length(fiber2)]);
FIBER1 = reshape(fiber1',[1,3,length(fiber1)]); %this is only used in the symmetrical case, i.e when 'min' or 'mean' option is called
%reshape amd tile filber 1 so as to eliminate the need for two nested for
%loops thus greatly increasing the computational efficiency. The goal is to
%have a 4D matrix with 1 row and 3 columns. Dimension 3 is a smearing of
%these columns to be as long as fiber2 so that each vector (1x3) in fiber1
%can be placed "on top" as in a row above the whole of fiber2. Thus dim 3
%is as long as fiber2 and dim 4 is as long as fiber1.
fiber1 = reshape(fiber1',[1,3,length(fiber1)]); %1x3xF1
fiber1 = repmat(fiber1,[length(FIBER2),1,1]); %F2x3xF1
fiber1 = permute(fiber1,[2,1,3]); %3xF2xF1
fiber1 = reshape(fiber1,[1,3,length(FIBER2),length(FIBER1)]);%1,3,F2,F1
mcd = mean(min(distance(fiber1, repmat(FIBER2,[1,1,1,length(FIBER1)]))));
if nargin > 2
fiber2 = reshape(fiber2',[1,3,length(fiber2)]); %1x3xF1
fiber2 = repmat(fiber2,[length(FIBER1),1,1]); %F2x3xF1
fiber2 = permute(fiber2,[2,1,3]); %3xF2xF1
fiber2 = reshape(fiber2,[1,3,length(FIBER1),length(FIBER2)]);%1,3,F2,F1
mcd2 = mean(min(distance(fiber2, repmat(FIBER1,[1,1,1,length(FIBER2)]))));
if strcmp(option,'mean')
mcd = mean([mcd mcd2]);
elseif strcmp(option,'min')
mcd = min([mcd mcd2]);
end;
end;
这是我在上面使用的 distance() 函数,在我的例子中,我使用了欧几里得距离,但你可以将它调整为最适合你的方法,只要它可以接受两个向量:
function Edist = distance(vector1,vector2)
%distance(vector1,vector2)
%
%provides the Euclidean distance between two input vectors. Vector1 and
%vector2 must be row vectors of the same length. The number of elements in
%each vector is the dimnesionality thereof.
Edist = sqrt(sum((diff([vector1;vector2])).^2));