function Bal = PathBal(Bal0, R, N)
% Octave/MATLAB function to track an account balance
% along all paths in a recombining binomial tree R
% of riskless returns.
% INPUTS:                                  (Example)
%   Bal0 = initial deposit                     (200)
%   R = riskless returns matrix              (N x N)
%   N = binomial tree height, must be >1         (3)
% OUTPUTS:
%   Bal = non-recombining tree array 
% EXAMPLE:
%  R=[1.025,0,0;1.03,1.02,0;1.035,1.025,1.01]
%  Bal=PathBal(200,R,3)
%
  Bal = zeros(1,2^(N+1)-1);  Bal(1) = Bal0;
  [nm,jm] = NJfromM(N); % recombining index functions
  for(m=1:2^N-1)
    Rnj=R(nm(m)+1,jm(m)+1);
    Bal(2*m)=Bal(m)*Rnj; Bal(2*m+1)=Bal(m)*Rnj;
  end
  return
end
