% 定义感知器算法 d=w1*x1+w2*x2+w3;
x=[0,0;0,1;1,0;1,-1];
class=[1,1,-1,-1];
%定义修改权重p
p=1;
% 初始化权重参数
w=[0,0,0];
%判别准则是错误次数err_count
err_count=1;
[N,M]=size(x);
%增广and规范化
x=[x,ones(N,1)];
xx=zeros(N,M+1);
for i=1:N
xx(i,:)=x(i,:)*class(i);
end
% 算法停止准则--错误次数为0
while(err_count>=1)
err_count=0;
for i=1:N
if dot(w,xx(i,:))<=0 %err
w=w+p*xx(i,:);
err_count=err_count+1;
else
w=w;
end
end
end
%画图and结果
xmin=0;
xmax=5;
ymin=-2;
ymax=4;
plot(x(1:2,1),x(1:2,2),'ro','MarkerFaceColor','r');
hold on
plot(x(3:4,1),x(3:4,2),'go','MarkerFaceColor','g');
hold on
x1=-2:4;
result_x2 =(-w(3)-w(2)*x1)/w(1);
plot(result_x2,x1,'-');
axis([xmin xmax ymin ymax]);
title('感知器算法');
xlabel('x1');
ylabel('x2');
legend('w1','w2','感知器分类线',-1)
disp(sprintf('最终权重值:w1=%d w2=%d w3=%d',w));
第二行的class是什么意思?
x=[0,0;0,1;1,0;1,-1];
class=[1,1,-1,-1];
%定义修改权重p
p=1;
% 初始化权重参数
w=[0,0,0];
%判别准则是错误次数err_count
err_count=1;
[N,M]=size(x);
%增广and规范化
x=[x,ones(N,1)];
xx=zeros(N,M+1);
for i=1:N
xx(i,:)=x(i,:)*class(i);
end
% 算法停止准则--错误次数为0
while(err_count>=1)
err_count=0;
for i=1:N
if dot(w,xx(i,:))<=0 %err
w=w+p*xx(i,:);
err_count=err_count+1;
else
w=w;
end
end
end
%画图and结果
xmin=0;
xmax=5;
ymin=-2;
ymax=4;
plot(x(1:2,1),x(1:2,2),'ro','MarkerFaceColor','r');
hold on
plot(x(3:4,1),x(3:4,2),'go','MarkerFaceColor','g');
hold on
x1=-2:4;
result_x2 =(-w(3)-w(2)*x1)/w(1);
plot(result_x2,x1,'-');
axis([xmin xmax ymin ymax]);
title('感知器算法');
xlabel('x1');
ylabel('x2');
legend('w1','w2','感知器分类线',-1)
disp(sprintf('最终权重值:w1=%d w2=%d w3=%d',w));
第二行的class是什么意思?