模式识别:根据判别函数对随机点进行分类

实验给出了三个判别函数:

d12 = 2*x1-3*x2-3

d13 = -10*x1 - x2 - 1

d23 = -x1 + x2 - 1

实验中利用matlab随机生成了100个二维点,利用上述判别函数将所有点分成了三类,代码和结果如下

clc
clear

a = 10;
b = 10;
n = 100;
cxd1 = a*rand(n,1)-5;
cxd2 = b*rand(n,1)-5;
cxd = [cxd1 cxd2];
subplot(121)
plot(cxd1,cxd2,'o');
xlabel('x1');
ylabel('x2');
title('random point');

d12 = 2.*cxd1-3.*cxd2-3;
d13 = -10.*cxd1 - cxd2 - 1;
d23 = -cxd1 + cxd2 - 1;

w1 = [];w2 = [];w3 = [];wir = [];
for i = 1:1:length(cxd)
    if(d12(i)>0 & d13(i)>0)
        w1 = [w1;cxd(i,:)];
    else if(d12(i)<0 & d23(i)>0)
            w2 = [w2;cxd(i,:)];
        else if(d13(i)<0 & d23(i)<0)
                w3 = [w3;cxd(i,:)];
            else
                wir = [wir,cxd(i,:)];
            end
        end
    end
end

subplot(122)
x = -5:1:5;
y1 = -(2/3).*x-1;
y2 = -10.*x - 1;
y3 = x+1;
plot(x,y1,'r');
hold on
plot(x,y2,'b');
plot(x,y3,'g');
axis([-5 5 -5 5]);
xlabel('x1');
ylabel('x2');
title('result');
if(~isempty(w1))
    plot(w1(:,1),w1(:,2),'o');
end
if(~isempty(w2))
    plot(w2(:,1),w2(:,2),'*');
end
if(~isempty(w3))
    plot(w3(:,1),w3(:,2),'^');
    legend('d12(x)=0','d13(x)=0','d23(x)=0','w1','w2','w3');
end
if(~isempty(wir))
    plot(wir(:,1),wir(:,2),'+');
    legend('d12(x)=0','d13(x)=0','d23(x)=0','w1','w2','w3','ir');
end
运行结果
posted @ 2019-10-27 11:10  昨夜昙花  阅读(22)  评论(0)    收藏  举报