Octave/Matlab - 无法/绘制数据

Spe*_*her 6 matlab octave

我有一个简单的数据文件,如下所示:

data.txt
34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下代码绘制其数据:

data = load('data.txt');
X = data(:, [1, 2]); y = data(:, 3);

plotData(X, y);

hold on;

xlabel('Exam 1 score')
ylabel('Exam 2 score')

legend('Admitted', 'Not admitted')
hold off;

pause;
Run Code Online (Sandbox Code Playgroud)

但是这给我带来了以下错误:

warning: legend: plot data is empty; setting key labels has no effect
error: legend: subscript indices must be either positive integers less than 2^31 or logicals
Run Code Online (Sandbox Code Playgroud)

没有任何东西被绘制.

我不明白什么是错的.工作目录很好八度.

我怎样才能解决这个问题?

非常感谢

Rah*_*man 8

你正在尝试安德鲁·吴(Andrew Ng)在课程上的第三周的机器学习课程作业.在ex2.m文件中,有一个函数plotData(X,y)的调用,它引用了plotData.m文件中写的函数.您可能认为plotData是八度音阶中的默认函数,但实际上您需要在plotData.m文件中实现该函数.这是我在plotData.m文件中的代码.

function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure 
%   PLOTDATA(x,y) plots the data points with + for the positive examples
%   and o for the negative examples. X is assumed to be a Mx2 matrix.

% Create New Figure
figure; hold on;

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
%               2D plot, using the option 'k+' for the positive
%               examples and 'ko' for the negative examples.
%

pos = find(y==1);
neg = find(y==0);
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);

% =========================================================================



hold off;

end
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您仔细阅读pdf,则PlotData.m代码位于pdf中.这是代码:

% Find Indices of Positive and Negative Examples
pos = find(y==1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);
Run Code Online (Sandbox Code Playgroud)


Dan*_*oks 3

1) X 是一个 5x2 数组,而 y 是一个 5x1 数组

2)plotData不是Matlab命令,请使用plot代替

尝试以下代码:

data = load('data.txt');
x1 = data(:, 1);
x2 = data(:,2);
y = data(:, 3);

plot(x1, y);
hold on
plot(x2,y);

xlabel('Exam 1 score')
ylabel('Exam 2 score')

legend('Admitted', 'Not admitted')
hold off;
pause;
Run Code Online (Sandbox Code Playgroud)