📄 test.m
字号:
clear all;clc
data = 3; %data=1,2,3 for the known data plot
if data == 1
load gauss2;
m = 2;
[n f] = size(gauss2);
d = f - 1;
data1 = gauss2(:,1:d);
%gauss2 classification
p = 1; q = 1;
for i = 1:n
if gauss2(i,3) == 0
class1(p,:) = data1(i,:);
p = p + 1;
else
class2(q,:) = data1(i,:);
q = q + 1;
end
end
figure % open a figure window
plot(class1(:,1),class1(:,2),'og');
hold on
plot(class2(:,1),class2(:,2),'+r');
hold off
% axis([-6 6 -6 6]); % fix the lengths of the axes
xlabel('Dimension 1');
ylabel('Dimension 2');
title('classes computed by k-Means');
elseif data == 2
load gauss3;
m = 3;
[n f] = size(gauss3);
d = f - 1;
data1 = gauss3(:,1:d);
%gauss3 classification
p = 1; q = 1; r = 1;
for i = 1:n
if gauss3(i,3) == 0
class1(p,:) = data1(i,:);
p = p + 1;
elseif gauss3(i,3) == 1
class2(q,:) = data1(i,:);
q = q + 1;
else
class3(r,:) = data1(i,:);
r = r + 1;
end
end
figure % open a figure window
plot(class1(:,1),class1(:,2),'og');
hold on
plot(class2(:,1),class2(:,2),'+r');
hold on
plot(class3(:,1),class3(:,2),'*b');
hold off
% axis([-6 6 -6 6]); % fix the lengths of the axes
xlabel('Dimension 1');
ylabel('Dimension 2');
title('classes computed by k-Means');
elseif data == 3
load iris;
m = 3;
[n f] = size(iris);
d = f - 1;
data1 = iris(:,1:d);
%iris classification
p = 1; q = 1; r = 1;
for i = 1:n
if iris(i,5) == 1
class1(p,:) = data1(i,:);
p = p + 1;
elseif iris(i,5) == 2
class2(q,:) = data1(i,:);
q = q + 1;
else
class3(r,:) = data1(i,:);
r = r + 1;
end
end
figure % open a figure window
plot(class1(:,1),class1(:,2),'og');
hold on
plot(class2(:,1),class2(:,2),'+r');
hold on
plot(class3(:,1),class3(:,2),'*b');
hold off
% axis([-6 6 -6 6]); % fix the lengths of the axes
xlabel('Dimension 1');
ylabel('Dimension 2');
title('classes computed by k-Means');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -