%RRI_MKBOOT_ORDER Generate permutation sample order
%
%   Usage: boot_order = rri_mkboot_order(num_cond, num_subj_lst, nboot)
%
%   Generates bootstrap samples according to whether the data matrix
%   includes one group or more, or if there are equal or differing
%   numbers of subjects must organize with proper numbers of subjects
%   within scans.
%
%   Used with bootstrap_pls program to estimate SE for saliences
%
%   See also K_MKBOOTPERM, RRI_BOOT_ORDER
%

%   I (num_cond) - number of conditions per subject in each group
%   I (num_subj_lst) - a list contains number of subjects in all groups
%   I (nboot) - number of bootstraps
%   O (boot_order) - an index matrix of new order for stacked datamat
%
%   edited version to make a boot_samp that has he same number of rows as
%   datamat and nboot columns....Here the first n1 rows are random numbers 
%   between 1 and n1 and the next n1+1...n1+n2 rows hold random numbers
%   between 1 and n2
%
%   June 24/97 edited to fix a very very infrequently occuring problem
%   When creating the vector 'resamp', it previously made no check to make
%   sure that ALL the values in this vector are not the same. It is fine
%   for most to be the same, but if all are the same, you will later get a
%   'Divide by zero' error in xcor when dividing by 'stdev'.  Stdev will
%   all be zero since all elements of rows are the same 
%
%   Written 6.18.96 by ARM (with moral support from NR)
%   Modified on 23-OCT-2002 by Jimmy Shen to allow any number of groups
%                               with different number of subjects
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function boot_order = rri_mkboot_order(num_cond, num_subj_lst, nboot)

    num_grp = length(num_subj_lst);

    %  initialize the perm_order to its size
    %
    boot_order = zeros(sum(num_subj_lst)*num_cond, nboot);

    %  loop through groups
    %
    for g=1:num_grp

        %  loop through nboot
        %
        for i=1:nboot

            n = num_subj_lst(g);
            span = sum(num_subj_lst(1:g-1)) * num_cond;

	    OK = 0;
	    while (OK == 0)

		%  resamp is an n by 1 matirx of random numbers in (1,n)
		%
		resamp=floor(rand(n,1)*n)+1;

		% temp=find(resamp==resamp(1,1));
		% [tsizer,tsizec]=size(temp);
		% if (tsizer < n)
		%     OK = 1;
		% end

		% the above commented code was replaced by the following
		%
		test=length(unique(resamp));

		% check to make sure there are more than n/2 people
		%
		if (test >= n/2 | test == n)
                    OK = 1;
                end

	    end			% while

	    %loop to make one column of boot_samp
	    %
	    for j=1:num_cond

		%  this puts the column vector of random numbers (resamp
		%  plus 0 first, then n1, then plus 2(n2),...) and thus
		%  makes a column when done scan times 
		%
		%  NOTE that the starting point and ending points are not
		%  so simple now...They do not go from 1 to n1
		%  and n1+1 to 2n1...etc anymore.  Instead the start and 
		%  end each have "scan*n1" added to them...This is to 
		%  keep the locations the random numbers go in the 
		%  boot_order after the initial group's data.
		%
		boot_order(1+n*(j-1)+span:n*j+span,i) = [resamp+n*(j-1)+span];

	    end
	end		% nboot
    end		%num_grp

    return;

