Matlab contour作图不显示的可能原因

复习Coursera上斯坦福Andrew Ng的Machine Learning课程,做到week 7的ex6时,发现ex6.m后两个作图出不来。

ex6.m有三次作图:一次是直线边界,两次曲线。这里显示不出的是后两个,本该显示蓝色边界的地方空白。而之前做题时正常可用。

其他版本的matlab运行正常,可能是因为Matlab新版本中个别函数有变动。问题应该就出在作图函数visualizeBoundary.m这里。

该函数代码如下:

function visualizeBoundary(X, y, model, varargin)
%VISUALIZEBOUNDARY plots a non-linear decision boundary learned by the SVM
%   VISUALIZEBOUNDARYLINEAR(X, y, model) plots a non-linear decision 
%   boundary learned by the SVM and overlays the data on it

% Plot the training data on top of the boundary
plotData(X, y)

% Make classification predictions over a grid of values
x1plot = linspace(min(X(:,1)), max(X(:,1)), 100)';
x2plot = linspace(min(X(:,2)), max(X(:,2)), 100)';
[X1, X2] = meshgrid(x1plot, x2plot);
vals = zeros(size(X1));
for i = 1:size(X1, 2)
   this_X = [X1(:, i), X2(:, i)];
   vals(:, i) = svmPredict(model, this_X);
end

% Plot the SVM boundary
hold on
contour(X1, X2, vals, [0 0], 'Color', 'b');

% % When running the original-version codes downloaded from Coursera,
% % if the boundary doesn't show up, try another parametre like [0.5,0.5]
% % instead of [0 0].
% % It seems there're certain shifts of the plot function 
% % in some newer versions of Matlab where [0 0] means "no width".

hold off;

end

问题显然就在contour上。中间的[0 0]参数之前没用过,修改了一下,改成[0.5 0.5],作图就正常了。可能是边界线型属性。
% %行注释是我加的。

这次的问题可能是因为Matlab新版本中个别函数有变动,另一个体现是上述图形的边界直接卡到了输入数据上,而没有留下一定空间。这一点跟之前也不太一样。

你可能感兴趣的:(Matlab contour作图不显示的可能原因)