function res = plot2features(tset, f1, f2)
% Plots tset samples on a 2-dimensional diagram
% using features f1 and f2
% tset - training set; the first column contains class label
% f1 - index of the first feature (mapped to horizontal axis)
% f2 - index of the second feature (mapped to vertical axis)
%
% res - matrix containing values of f1 and f2 features
% plotting parameters for different classes
% restriction to 8 classes seems reasonable
pattern(1,:) = 'ks';
pattern(2,:) = 'rd';
pattern(3,:) = 'mv';
pattern(4,:) = 'b^';
pattern(5,:) = 'gs';
pattern(6,:) = 'md';
pattern(7,:) = 'mv';
pattern(8,:) = 'g^';
res = tset(:, [f1, f2]);
% extraction of all unique labels used in tset
labels = unique(tset(:,1));
% create diagram and switch to content preserving mode
figure;
hold on;
for i=1:size(labels,1)
idx = tset(:,1) == labels(i);
plot(res(idx,1), res(idx,2), pattern(i,:));
end
hold off;
end
我编写了这个函数,我想在MATLAB中的一个寡妇中显示多个plot2featutes()
。
我尝试了以下内容,
subplot(2,2,1);plot2features(train, 2, 3);
subplot(2,2,2);plot2features(train, 2, 3);
subplot(2,2,3);plot2features(train, 2, 3);
subplot(2,2,4);plot2features(train, 2, 3);
那不起作用。
答案 0 :(得分:1)
如评论中所述,问题在于您的函数中的行figure;
。如果没有参数,figure
会创建一个新的数字,然后将焦点切换到该数字。你的功能应该更像这样:
function res = plot2features(tset, f1, f2)
% Plots tset samples on a 2-dimensional diagram
% using features f1 and f2
% tset - training set; the first column contains class label
% f1 - index of the first feature (mapped to horizontal axis)
% f2 - index of the second feature (mapped to vertical axis)
%
% res - matrix containing values of f1 and f2 features
% plotting parameters for different classes
% restriction to 8 classes seems reasonable
pattern = {'ks'; 'rd'; 'mv'; 'b^'; 'gs'; 'md'; 'mv'; 'g^'};
res = tset(:, [f1, f2]);
% extraction of all unique labels used in tset
labels = unique(tset(:,1));
% create diagram and switch to content preserving mode
for ii = 1:size(labels,1)
if ii == 1
hold off
else
hold on
end
idx = tset(:,1) == labels(ii);
plot(res(idx,1), res(idx,2), pattern{ii});
end
hold off;
end
我对您的代码进行了三处更改:
figure
并将其替换为在第一次传递时转为hold off
的测试,以便创建一个新轴,然后在这些轴内绘制后续绘图。i
(即sqrt(-1))更改为ii
以避免歧义pattern
更改为一个单元格数组,除了我自己的个人美学外无其他理由。这样打电话:
figure
subplot(2,2,1);plot2features(train, 2, 3);
subplot(2,2,2);plot2features(train, 2, 3);
subplot(2,2,3);plot2features(train, 2, 3);
subplot(2,2,4);plot2features(train, 2, 3);