function [nm,jm] = NJfromM(N)
% Octave/MATLAB function to compute indexes (n,j)
% in a recombining binomial tree from the index m
% in a non-recombining tree, by induction.
% INPUTS:                             (Example)
%   N = tree height, must be >0           (3)
% OUTPUTS:
%   nm = n(m), time step
%   jm = j(m), recombining state at time step n(m)
% EXAMPLE:
%   [nm,jm] = NJfromM(3);
%
  % Initialize output arrays with zeros
  nm = zeros(1,2*2^N-1); %...so  nm(1)==0;
  jm = zeros(1,2*2^N-1); %...so  jm(1)==0;
  for m=1:2^N-1  % all m indices before the last row
    nm(2*m)=nm(m)+1;  nm(2*m+1)=nm(m)+1; % increment
    jm(2*m)=jm(m);    jm(2*m+1)=jm(m)+1; % bifurcate
  end % nm(m) and jm(m) now hold (n(m),j(m)), all m
  return
end
