function [ ws ] = prob_4_5(Niter) % PROB_4_5 - % % 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. % % Note: I am using the update formula on page 68 equation (4.30) to % perform the optimization. % %----- data = [ 0 1 -1 2; 1 1 1 1; 2 1 1 1; -1 1 0 -1; ]; class = [ 1 2 2 3 ]; d = 0.5; % Weights that sparate the three classes: %ws = rand( 3, size(data,2)+1 ); % Row one is for class #1; row two is for class #2, etc. ws = zeros( 3, size(data,2)+1 ); % Row one is for class #1; row two is for class #2, etc. % % Code begins: %--- % Append +1 to all data: data = [ data, ones(size(data,1),1) ]; w_history = zeros( Niter, size(ws,2) ); for ni=0:Niter+1, % Select a sample: si = mod( ni, size(data,1) ) + 1; % Get feature vector and the true classification: y_mi = data(si,:).'; tc = class(si); % Find the predicted classification: [mTmp,pc] = max( ws * y_mi + d ); % Adjust linear weights if there is a misclassification: % e_i = 1; % Using fixed increment update... if( pc ~= tc ) wstp = ws(tc,:) + e_i * y_mi.'; wspp = ws(pc,:) - e_i * y_mi.'; else wstp = ws(tc,:); wspp = ws(pc,:); end % Prepare for next iteration: ws(tc,:) = wstp; ws(pc,:) = wspp; end