function bootsample = resampling (x, M, var)
% Resampling -- Generation of a Bootstrap data sample
%
%  Usage
%    bootsample = Resampling(x,M) 
%
%  Inputs
%    x: (NxD) data matrix
%       the rows giving the different individuals and
%       the columns giving the different attributes/variables
%    M: Size of the bootstrap data sample
%
%  Outputs
%    bootsample: (Mx1) cell of (NxD) data matrix.
%
%  Description
%    The matrix data 'bootsample' contains sampling data obtained by making
%    M independent draws with replacements in the original data sample.
%    Again, rows give the individuals, while columns give the attributes.
%
%  References
%    Efron, B. (1979).
%    Bootstrap Methods: Another Look at the Jacknife.
%    Annals of Statistics.
%
% See Also
%    BootTest, BootIC, BootUniv

  % Size of the Data Matrix
  [N, D] = size(x);

  % Default value
  if nargin < 3
    var = zeros (D, 1);
  end

  % for each bootstap set
  bootsample = cell(M, 1);
  for k=1:M,

    % for each draw
    bootsample{k} = zeros (N, D);
    for l=1:N,

      draw = zeros(1, D);
      X = x;

      % for each structural variable
      for n=find(var == 1),
        
        % find population
        pop = unique (X(:, n));
        nb_pop = length (pop);
        
        % draw element
        draw(n) = pop(floor(1 + rand * nb_pop));

        % update subset
        X = X(X(:, n) == draw(n), :);

      end

      % for non structural variables
      draw(var == 0) = X(floor(1 + rand *rows (X)), var == 0);

      bootsample{k}(l, :) = draw;

    end

  end
  
% Copyright (c) 2008. Laurent Mazet
%      
% Built Monday, August 28, 2008 14:55:56
% This is Copyrighted Material

% Local Variables: **
% mode: Matlab **
% End: **