MATLAB Answers


Simple Perceptron algorithm in matlab: cannot draw the classifier line

Asked by Hieu Nguyen on 16 Sep 2018
Latest activity Commented on by Hieu Nguyen on 16 Sep 2018
I am new to Matlab and I am trying to test my learning curve by writing the perceptron algorithm from scratch. I have seen this in R but I think it is fun to try out Matlab. It seems to me that I cannot draw the classifier line.
load data1.mat
w_0 = [1;-1];
for iteration = 1 : 100 %<- I do not know how to define a convergence or stopping criteria
for ii = 1 : size(X,2) %cycle through training set
if sign(w'.*X(:,ii)) <=0 %wrong decision?
w = w + X(:,ii) * y(ii); %then add (or subtract) this point to w
x1 = [-1,b];
x2 = [1,d];
b = w(1)/w(2);
d = -w(1)/w(2);
hold on;plot(x1,x2);hold off
My data set ("data1.mat") has X as the feature matrix of 40x2 and y. I want to plot the boundary line i.e the line w^Tx = w_1*x_1 + w_2*x_2 = 0. I just choose 2 points p1 = (a,b) and p2 = (c,d). Since my data limits x_1 and x_2 between -1 and 1, I choose a = 1 and c = -1 and find out b and d.
But still, the syntax looks normal to me but I have an error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in Myperceptron (line 12)
x1 = [-1,b];


There is no reference to the variable b. I can only assume that it is loaded from data1.mat file. You need to make sure b is scalar.
I fixed the code. I moved the definitions of b and d before x1 and x2 but there is still not line drawn from the plot(x1,x2)

Sign in to comment.

0 Answers