找到每个“类”的测试向量的
SSD (sum of squared differences),并使用SSD最少的那个.
这里有一些代码:我在你提供的测试向量的末尾加了一个0,因为它只有9位,而类有10位.
CLASSES = [1,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,1
0,1,1,1,1,1,1,1,1,0
0,1,0,0,0,0,0,0,0,0];
TEST = [0.8,0,0,0,0.6,0,0.1,0,0,0];
% Find the difference between the TEST vector and each row in CLASSES
difference = bsxfun(@minus,CLASSES,TEST);
% Class differences
class_diff = sum(difference.^2,2);
% Store the row index of the vector with the minimum difference from TEST
[val CLASS_ID] = min(class_diff);
% Display
disp(CLASSES(CLASS_ID,:))
为了便于说明,差异如下所示:
0.2 0 0 0 -0.6 0 -0.1 0 0 0
-0.8 0 0 0 -0.6 0 -0.1 0 0 1
-0.8 1 1 1 0.4 1 0.9 1 1 0
-0.8 1 0 0 -0.6 0 -0.1 0 0 0
并且每个类与TEST的距离如下所示,class_diff:
0.41
2.01
7.61
2.01
显然,第一个是最佳匹配,因为它具有最小的差异.