function Xbar = NRTinject(X,N)
% Octave/MATLAB function to inject values from a
% recombining binomial tree X with depth N, stored
% in an (N+1)x(N+1) matrix, into a non-recombining
% tree stored in an array indexed by m=1:2*2^N-1.
% INPUTS:                             (Example)
%   X = recombining tree matrix       (4x4 lower)
%   N = tree height, must be >0           (3)
% OUTPUTS:
%   Xbar = output array, Xbar(m)=X(n(m)+1,j(m)+1)
% EXAMPLE:
%   X = [1,0,0,0; 2,3,0,0;4,5,6,0;7,8,9,10];
%   Xbar = NRTinject(X,3);
%
  [nm,jm] = NJfromM(N); % get indexing functions
  for m=1:2*2^N-1  % all m indices including last row
    Xbar(m) = X(nm(m)+1,jm(m)+1); % no 0 index: add 1 
  end 
  return
end
