function [ output_args ] = percep_noprints( X, y, w, eta, max_iterations )

%Given X and y this function trains a perceptron.
%If w is not of the right dimension, it is initialized
%randomly.
%X = input patterns
%y = class labels of input patterns
%w = initial weight vector
%eta = step size
%max_iterations = maximum number of iterations
%author: gregor josse

[rows, cols] = size(X);
%add constant 1-vector as column to data matrix
X = [ones(rows,1),X];
%compute original classification labels (yhat)
if(size(w) ~= cols+1)
    disp('initializing random [0,1] weight vector:')
    w = rand(cols+1,1);
end
yhat = sign(X*w);
iterations = 0;
while(sum(yhat~=y) > 0 && iterations < max_iterations)
    %vector consisting of all indices of misclassified patterns
    wrongly_class_indices = find(yhat~=y);
    %pick one of these indices randomly
    if(length(wrongly_class_indices) == 1)
        index = wrongly_class_indices(1);
    else
        index = randsample(wrongly_class_indices,1);
    end
    %the corresponding pattern transposed (-> column vector)
    pattern = X(index,:)';
    %adjust weight vector: w_i <- w_i + eta*y(index)*X(i,index)
    w = w + eta * y(index) * pattern;
    yhat = sign(X*w);
    iterations = iterations + 1;
end

fprintf('classification result:')
fprintf('\n')
fprintf('%d ',yhat)
fprintf('\n')
fprintf('correct classification:')
fprintf('\n')
fprintf('%d ',y)
fprintf('\n')
end

