倍频程/Matlab-无法/打印数据

倍频程/Matlab-无法/打印数据,matlab,octave,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 我正试图用以下代码绘制它的数据: data = load('data.

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

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
我正试图用以下代码绘制它的数据:

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;
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;
但是,这给我带来了以下错误:

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
什么都没有策划

我不明白怎么了。工作目录的八度音阶很好

我怎样才能解决这个问题

非常感谢

1)X是5x2阵列,而y是5x1阵列

2) plotData不是Matlab命令,请改用plot

请尝试以下代码:

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;
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;

如果仔细阅读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);

P>你在第三周尝试Andrew Ng在CurSera的机器学习课程上的作业。在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

您必须从PDF文档中键入上述代码(而不是复制和粘贴)到plotData.m文件。

我认为您需要在那里使用单元格数组-
图例({'accounted','not accounted'})
。我试过了,但仍然不起作用:(打印
X
y
时会得到什么?它们真的是空的吗?另外,尝试使用而不是。Load用于存储在文件中的matlab变量。
plotData
不是标准的倍频程函数,看看这个。它的子图就可以了。您需要在plotData中实现函数plotData。我阅读文档。请如果这最有帮助,请将其视为公认的答案。
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