I will post the code first! Study it slowly!
function [out1,out2] = learnp(varargin)%LEARNP Perceptron weight/bias learning function.%% <a href="matlab:doc learnp">learnp</a> is the perceptron weight/bias learning function.%% <a href="matlab:doc learnp">learnp</a>(W,P,Z,N,A,T,E,gW,gA,D,LP,LS) takes several inputs,% W - SxR weight matrix (or b, an Sx1 bias vector).% P - RxQ input vectors (or ones(1,Q)).% Z - SxQ weighted input vectors.% N - SxQ net input vectors.% A - SxQ output vectors.% T - SxQ layer target vectors.% E - SxQ layer error vectors.% gW - SxR gradient with respect to performance.% gA - SxQ output gradient with respect to performance.% D - SxS neuron distances.% LP - Learning parameters, none, LP = [].% LS - Learning state, initially should be = [].% and returns,% dW - SxR weight (or bias) change matrix.% LS - New learning state.%% <a href="matlab:doc learnp">learnp</a>(CODE) returns useful information for each CODE string:% 'pnames' - Returns names of learning parameters.% 'pdefaults' - Returns default learning parameters.% 'needg' - Returns 1 if this function uses gW or gA.%% Here we define a random input P and error E to a layer% with a 2-element input and 3 neurons.%% p = rand(2,1);% e = rand(3,1);%% LEARNP only needs these values to calculate a weight change.%% dW = <a href="matlab:doc learnp">learnp</a>([],p,[],[],[],[],e,[],[],[],[],[])%% See also LEARNPN, NEWP, ADAPT, TRAIN.% Mark Beale, 1-31-92% Revised 11-31-97, MB% Copyright 1992-2010 The MathWorks, Inc.% $Revision: 1.1.6.8 $ $Date: 2010/04/24 18:09:27 $%% =======================================================% BOILERPLATE_START% This code is the same for all Learning Functions. persistent INFO; if isempty(INFO), INFO = get_info; end if (nargin < 1), nnerr.throw('Not enough arguments.'); end in1 = varargin{1}; if ischar(in1) switch in1 case 'info' out1 = INFO; case 'check_param' out1 = check_param(varargin{2}); otherwise, try out1 = eval(['INFO.' in1]); catch me nnerr.throw(['Unrecognized first argument: ''' in1 '''']) end end else [out1,out2] = apply(varargin{:}); endendfunction sf = subfunctions sf.apply = @apply;endfunction v = fcnversion v = 7;end% BOILERPLATE_END%% =======================================================function info = get_info info = nnfcnLearning(mfilename,'Perceptron',... fcnversion,subfunctions,false,true,true,false,[]); % TODO - Indicate that it requires errorendfunction err = check_param(param) err = '';endfunction [dw,ls] = apply(w,p,z,n,a,t,e,gW,gA,d,lp,ls) dw = e*p';end