📄 avq.m
字号:
% ==========================================================
%
% Neural Networks A Classroom Approach
% Satish Kumar
% Copyright Tata McGraw Hill, 2004
%
% MATLAB code that implements the AVQ algorithm
% Reference: Table 12.4;Page 544
%
% ==========================================================
% Program for AVQ Clustering: m clusters in n dimensions
m = 2; % Generate two clusters
fid=fopen('./avqtest.dat','r'); % Open data file
pat = fscanf(fid,'%f %f',[3 inf]);
fclose(fid);
[n,Q]=size(pat); % Dimension n, and number of data Q
w = [3 -2 % Initial weight matrix w
0 3
0 5];
figure % Plot the clusters
plot3(pat(1,:), pat(2,:), pat(3,:), 'b.');
grid on;
hold on;
axis([-2 4 -2 5 -2 5]);
% For weight storage
xindex = 1;
yindex = 1;
for i=1:Q % for each data point
minindex = -1; % reset the minimum distance index
mindist = 1000; % set the mindist variable to a large number
for j=1:m % check the distance to each cluster codebook
dist = 0;
for k=1:n
dist = dist + (pat(k,i)-w(k,j))^2;
end
dist = sqrt(dist);
if dist < mindist
minindex = j;
mindist = dist;
end
end
eta = 0.1*(1-(i/(2*Q))); % Update the learning rate
for k=1:n % Update the winning weight vector
w(k,minindex) = w(k,minindex) + eta*(pat(k,i) - w(k,minindex));
end
if minindex == 1
w1(xindex,1) = w(1,minindex);
w1(xindex,2) = w(2,minindex);
w1(xindex,3) = w(3,minindex);
xindex = xindex + 1;
elseif minindex == 2
w2(yindex,1) = w(1,minindex);
w2(yindex,2) = w(2,minindex);
w2(yindex,3) = w(3,minindex);
yindex = yindex + 1;
end
end
plot3(w1(:,1),w1(:,2),w1(:,3),'k-');
plot3(w2(:,1),w2(:,2),w2(:,3),'k-');
for k=1:m
plot3(w(1,k),w(2,k),w(3,k),'r^');
end
xlabel('x');
ylabel('y');
zlabel('z');
rotate3d on
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -