Octave/Matlab - Cannot/plot data
You are trying the assignment on week three in machine learning course by Andrew Ng on coursera. There in ex2.m file, there is a call to function plotData(X,y) which refers to the function written in plotData.m file. You may thought that plotData is a default function in octave, but you actually need to implement that function in plotData.m file. Here is my code in plotData.m file.
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
If you read the pdf carefully, the PlotData.m codes are is in the pdf. Here is the code:
% 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);
1) X is a 5x2 array while y is a 5x1 array
2) plotData isn't a Matlab command, use plot instead
Try the following code:
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;