Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave/Matlab - Cannot/plot data

Tags:

matlab

octave

I have a simple data file looking like this:

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

And I am trying to plot its data using the following code:

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;

However this bring me the following errors:

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

And nothing gets plotted.

I don't understand what's wrong. The working directory is fine in octave.

How can I fix this?

Many thanks

like image 759
Spearfisher Avatar asked Apr 07 '15 21:04

Spearfisher


3 Answers

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
like image 123
Rahat Zaman Avatar answered Oct 23 '22 10:10

Rahat Zaman


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);
like image 7
clumsycat Avatar answered Oct 23 '22 10:10

clumsycat


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;
like image 3
Daniel Brooks Avatar answered Oct 23 '22 12:10

Daniel Brooks