function [ f_out  ] = BRF_CompactForest(f_out)
%   BRF_CompactForest     Compacts forest so evaluations are quick
%

%	$LastChangedBy: alistair $
%	$LastChangedDate: 2012-05-30 12:21:30 +0100 (Wed, 30 May 2012) $
%	$Revision: 21 $
%	Originally written on MACI64 by Louis Mayaud, 25-April-2012 14:05:26
%	Contact: alistairewj@gmail.com




forests = f_out.forests;
% priors = f_out.priors;
% e1 = f_out.e1;
NaNNbre = f_out.TrainNaN;
% family = f_out.Family;
xtrain = f_out.xtrain;
xtrain_rk = tiedrankrelative(xtrain,true(size(xtrain,1),1));
Ntrees = size(forests,3)-1;
nan_placement = false(2,Ntrees,size(forests,1));

for i = 1:size(forests,1)
    % determine missing value assignments for each index on training set
    forest = reshape(forests(i,:,:),[size(forests,2),size(forests,3)]);
    
    % first split value [0,1] * number of non-nan values
    i1 = forest(1,1:Ntrees);
    i4 = forest(4,1:Ntrees);
    i7 = forest(7,1:Ntrees);
    i1_NaN = NaNNbre(i1);
    
    obs1train = sum(bsxfun(@gt, xtrain_rk(:,i1),i4.*i1_NaN),1);
    obs1train = i1_NaN.*i7 < obs1train;
    nan_placement(1,:,i) = obs1train;
    
    
    
    i2 = forest(2,1:Ntrees);
    i5 = forest(5,1:Ntrees);
    i8 = forest(8,1:Ntrees);
    i2_NaN = NaNNbre(i2);
    
    obs2train = i2_NaN.*i8 < sum(bsxfun(@gt, xtrain_rk(:,i2),i5.*i2_NaN),1);
    nan_placement(2,:,i) = obs2train;

end

f_out.nan_placement = nan_placement;

end