function [ w_i, data ] = prob_4_6(Niter) % PROB_4_6 - % % Written by: % -- % John L. Weatherwax 2005-08-04 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- data = [ 1 1 -1 0 2; 0 0 1 2 0; -1 -1 1 1 0; 4 0 1 2 1; -1 1 1 1 0; -1 -1 -1 1 0; -1 1 1 2 1 ]; class = [ 2 1 2 1 1 1 2 ]; M = length(class); d = 0.1; w_i = [ 1 1 1 -1 -1 1 ]; % % Code begins: %--- % Append +1 to all data: data = [ data, ones(size(data,1),1) ]; % Negate the feature vector from class 2: ind = find( class==2 ); data(ind,:) = -data(ind,:); clear ind; w_history = zeros( Niter, length(w_i) ); for i=0:Niter+1, e_i = 1; % Find the set J (the missclassified samples): [J] = find( data * (w_i.') < d ); J % Update the weight vector (usin the many at a time algorithm): if( ~isempty(J) ) w_ip1 = w_i; for j=1:length(J), w_ip1 = w_ip1 + e_i * data( J(j), : ); end else w_ip1 = w_i; end % Save w iterates: w_history( i+1, : ) = w_i; % Prepare for next iteration: w_i = w_ip1; end w_history(end-4:end,:)