function [v,a] = feedforward(in,W)
%compute activation for a feedforward network
%using tanh activation function (sigmoid between +/-1)

%W: cell array of weight matrices for all layers
%in: activation vector (column) for input layer

M = length(W); %number of layers
v = cell(M,1); %vectors of input activations, one layer per cell
a = cell(M,1); %vectors of output activations, one layer per cell
for m = 1:M
    if m==1
        prevout = [in;1]; %input to network, plus pseudo-output for bias term
    else
        prevout = [a{m-1};1]; %output of layer m-1, plus pseudo-output for bias term
    end
    v{m} = W{m}*prevout; %input at layer m, based on output of previous layer
    a{m} = tanh(v{m}); %output at layer m, from transfer function
end