function [Ac,N,LWZ,IWZ,BZ]=calca1(net,PD,Ai,Q)
%CALCA1 Calculate network signals for one time step.
%
%	Syntax
%
%	  [A,N,LWZ,IWZ,BZ] = CALCA1(NET,PD,Ai,Q)
%
%	Warning!!
%
%	  This function may be altered or removed in future
%	  releases of the Neural Network Toolbox. We recommend
%	  you do not write code which calls this function.

% Mark Beale, 11-31-97
% Copyright (c) 1992-1998 by The MathWorks, Inc.
% $Revision: 1.3 $

% Concurrent biases
BZ = cell(net.numLayers,1);
ones1xQ = ones(1,Q);
for i=net.hint.biasConnectTo
  BZ{i} = net.b{i}(:,ones1xQ);
end

% Signals
IWZ = cell(net.numLayers,net.numInputs);
LWZ = cell(net.numLayers,net.numLayers);
N = cell(net.numLayers);
Ac = [Ai cell(net.numLayers,1)];

% Simulation
ts2 = net.numLayerDelays + 1;
for i=net.hint.simLayerOrder
  
  layer = net.layers{i};
  
  % Input Weights -> Weighed Inputs
  inputInds = find(net.inputConnect(i,:));
  for j=inputInds
    IWZ{i,j} = feval(net.inputWeights{i,j}.weightFcn,net.IW{i,j},PD{i,j});
  end
    
  % Layer Weights -> Weighted Layer Outputs
  layerInds = find(net.layerConnect(i,:));
  for j=layerInds
	Ad = cell2mat(Ac(j,ts2-net.layerWeights{i,j}.delays)');
    LWZ{i,j} = feval(net.layerWeights{i,j}.weightFcn,net.LW{i,j},Ad);
  end
  
  % Net Input Function -> Net Input
  biasInds = find(net.biasConnect(i));
  Z = [IWZ(i,inputInds) LWZ(i,layerInds) BZ(i,biasInds)];
  N{i} = feval(layer.netInputFcn,Z{:});
  
  % Transfer Function -> Layer Output
  Ac{i,ts2} = feval(layer.transferFcn,N{i});
end

% Convert Ac to A
if net.numLayerDelays
  Ac = Ac(:,ts2);
end
