📄 nnd4db.m
字号:
'color',nnltyell)
set(w1_text,...
'color',nnltgray)
set(w2_text,...
'color',nnltgray)
set(b_text,...
'color',nnltgray)
nntxtchk
set(db1,...
'color',nndkblue)
set(cross,...
'color',nndkblue)
dots = get(dots_ptr,'userdata');
T = get(T_ptr,'userdata');
for i=1:length(T)
set(dots(i),...
'facecolor',[1 1 1]-T(i))
end
set(fig,...
'pointer','circle',...
'WindowButtonMotionFcn',[me '(''movept2'')'],...
'WindowButtonUpFcn',[me '(''setpt2'')'])
% EDIT INPUT/TARGET DOT
else
P = get(P_ptr,'userdata');
q = size(P,2);
for i=1:q
if sqrt(sum(sum((P(:,i)-[x; y]).^2))) < 0.15
T = get(T_ptr,'userdata');
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
dots = get(dots_ptr,'userdata');
set(dots(i),...
'facecolor',nnltyell,...
'edgecolor',nnltyell);
set(db_line,...
'color',nndkblue)
A = nndhardlim(w*P+b*ones(1,q));
if all(A == T)
col = nndkgray;
else
col = nnred;
end
set(w_arrow,...
'color',col)
set(db1,...
'color',nndkblue)
set(db2,...
'color',nndkblue)
set(cross,...
'color',nndkblue)
delete(dots(i));
t = T(i);
T(i) = [];
P(:,i) = [];
dots(i) = [];
for i=1:length(T)
set(dots(i),...
'facecolor',[1 1 1]-T(i))
end
set(T_ptr,'userdata',T);
set(P_ptr,'userdata',P);
set(dots_ptr,'userdata',dots);
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',t);
break;
end
end
end
% NEW DOTS
else
pt = get(t1_axis,'currentpoint');
x = pt(1);
y = pt(3);
% BLACK DOT
if (x >= -.15) & (x <= .15) & (y >= -.15) & (y <= .15)
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',1);
else
pt = get(t2_axis,'currentpoint');
x = pt(1);
y = pt(3);
% WHITE DOT
if (x >= -.15) & (x <= .15) & (y >= -.15) & (y <= .15)
set(fig,...
'pointer','circle',...
'WindowButtonUpFcn',[me '(''setdot'')'])
set(dot_ptr,'userdata',0);
end
end
end
%==================================================================
% Set decision dot.
%
% ME('setdot')
%==================================================================
elseif strcmp(cmd,'setdot') & (fig) & (nargin == 1)
pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*2)/2;
y = pt(3);
y = round(y*2)/2;
dot = get(dot_ptr,'userdata');
if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
w = get(w_ptr,'userdata');
b = get(b_ptr,'userdata');
dots = get(dots_ptr,'userdata');
q = length(dots)+1;
deg = pi/180;
angle = [0:5:360]*deg;
cx = cos(angle)*0.15;
cy = sin(angle)*0.15;
set(fig,'nextplot','add');
axes(v_axis);
a = nndhardlim(w*[x;y]+b);
if (a == dot)
col = nndkgray;
else
col = nnred;
end
dots(q) = fill(cx+x,cy+y,[1 1 1]-dot,...
'edgecolor',col,...
'erasemode','none',...
'linewidth',2);
set(fig,'nextplot','new');
P = [P [x;y]];
T = [T dot];
set(P_ptr,'userdata',P);
set(T_ptr,'userdata',T);
set(dots_ptr,'userdata',dots);
cmd = 'update';
end
set(fig,...
'pointer','arrow',...
'WindowButtonUpFcn','')
%==================================================================
% Move decision boundary point #1.
%
% ME('movept1')
%==================================================================
elseif strcmp(cmd,'movept1') & (fig) & (nargin == 1)
pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*4)/4;
y = pt(3);
y = round(y*4)/4;
% MOVE DECISION POINT #1
if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
pt = get(pt_ptr,'userdata');
pt(1,:) = [x y];
set(pt_ptr,'userdata',pt);
end
%==================================================================
% Respond to set decision boundary point #1.
%
% ME('setpt1')
%==================================================================
elseif strcmp(cmd,'setpt1') & (fig) & (nargin == 1)
set(fig,...
'WindowButtonMotionFcn','',...
'WindowButtonUpFcn','',...
'pointer','arrow')
pt = get(pt_ptr,'userdata');
set(db1,...
'xdata',pt(1,1),...
'ydata',pt(1,2),...
'color',nndkblue)
cmd = 'update';
%==================================================================
% Move decision boundary point #1.
%
% ME('movept2')
%==================================================================
elseif strcmp(cmd,'movept2') & (fig) & (nargin == 1)
pt = get(v_axis,'currentpoint');
x = pt(1);
x = round(x*4)/4;
y = pt(3);
y = round(y*4)/4;
% MOVE DECISION POINT #1
if (x >= -p_max) & (x <= p_max) & (y >= -p_max) & (y <= p_max)
pt = get(pt_ptr,'userdata');
pt(2,:) = [x y];
set(pt_ptr,'userdata',pt);
end
%==================================================================
% Respond to set decision boundary point #2.
%
% ME('setpt2')
%==================================================================
elseif strcmp(cmd,'setpt2') & (fig) & (nargin == 1)
set(fig,...
'WindowButtonMotionFcn','',...
'WindowButtonUpFcn','',...
'pointer','arrow')
pt = get(pt_ptr,'userdata');
set(db2,...
'xdata',pt(2,1),...
'ydata',pt(2,2),...
'color',nndkblue)
cmd = 'update';
%==================================================================
end
%==================================================================
% Respond to request to update displays.
%
% ME('update')
%==================================================================
if strcmp(cmd,'update')
% GET DATA
b = get(b_ptr,'userdata');
pt = get(pt_ptr,'userdata');
P = get(P_ptr,'userdata');
T = get(T_ptr,'userdata');
% UPDATE WEIGHTS & DECISION BOUNDARY
w_dir = [pt(1,2)-pt(2,2),pt(2,1)-pt(1,1)];
w = w_dir/sqrt(sum(sum(w_dir.^2)))*2;
b = -w*pt(1,:)';
if (w(1) ~= 0)
pp2 = [-p_max p_max];
pp1 = -(w(2)*pp2+b)/w(1);
if (pp1(1) < -p_max | pp1(1) > p_max)
pp1(1) = p_max*sign(pp1(1));
pp2(1) = -(w(1)*pp1(1)+b)/w(2);
end
if (pp1(2) < -p_max | pp1(2) > p_max)
pp1(2) = p_max*sign(pp1(2));
pp2(2) = -(w(1)*pp1(2)+b)/w(2);
end
elseif (w(2) ~= 0)
pp1 = [-p_max p_max];
pp2 = -(w(1)*pp1+b)/w(2);
else
pp1 = [0 0];
pp2 = [0 0];
end
% NEW BOUNDARY
set(db_line,...
'xdata',pp1,...
'ydata',pp2,...
'color',nndkblue)
% NEW WEIGHT VECTOR
set(fig,'nextplot','add')
axes(v_axis)
w_arrow = get(w_arrow_ptr,'userdata');
delete(w_arrow);
[R,Q] = size(P);
%A = hardlim(w*P+b*ones(1,Q));
A = nndhardlim(w*P+b*ones(1,Q));
if all(A == T)
col = nndkgray;
else
col = nnred;
end
w_arrow = nndrwvec(w(1),w(2),2,0.2,col,'W','none');
set(w_arrow_ptr,'userdata',w_arrow)
set(fig,'nextplot','new')
% We refresh the cross
set(cross,...
'color',nnred)
set(cross,...
'color',nndkblue)
% REFRESH DOTS
dots = get(dots_ptr,'userdata');
T = get(T_ptr,'userdata');
for i=1:length(T)
%a = hardlim(w*P(:,i)+b);
a = nndhardlim(w*P(:,i)+b);
if (a == T(i))
col = nndkgray;
else
col = nnred;
end
% We change color to refresh dots
set(dots(i),...
'facecolor',[0 0 0],...
'edgecolor',nndkblue)
set(dots(i),...
'facecolor',[1 1 1]-T(i),...
'edgecolor',col)
end
% NEW PARAMETER VALUES
set(w1_text,...
'string',sprintf('%5.3g',w(1)),...
'color',nndkblue)
set(w2_text,...
'string',sprintf('%5.3g',w(2)),...
'color',nndkblue)
set(b_text,...
'string',sprintf('%5.3g',b),...
'color',nndkblue)
nntxtchk
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -