You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: bst_permtest.m
    path of file: ./repos/brainstorm3/toolbox/math
    
    the code of the file until where you have to start completion: function [pv,S0,nGoodA,nGoodB,PS] = bst_permtest(A, B, TestType, dimPerm, nPerm, tails, isZeroBad)
% PERMTEST: Generic randomization test.
%
% USAGE:  [pv,S0,nGoodA,nGoodB,PS] = bst_permtest(A, B, TestType, dimPerm, nPerm, tails, isZeroBad)
%
% INPUTS:
%    - A, B     : [Ma x Mb...] Variables to test   
%    - TestType : {'ttest_equal', 'ttest_unequal', 'ttest_paired', 'wilcoxon_paired', 'wilcoxon', 'signtest'}
%         ttest_equal   : Student's t-value, equal variance
%         ttest_unequal : Student's t-value, unequal variance
%         ttest_paired  : Student's t-value, paired data
%         signtest      : Sign of the differences (paired)
%         wilcoxon      : Signed ranks (paired)
%         absmean       : Difference of absolute values of means
%    - dimPerm   : Dimension to permute (ie. subjects)
%    - nPerm     : Number of permutations
%    - tails     : 'one+' (X1>X2), 'one-' (X1<X2), 'two' (X1<X2 or X1>X2)
%    - isZeroBad : If 1, excludes zeros from all the calculations
%
% OUTPUTS:
%    - pv    : [Ma x Mb...] p-values of the observed data, computed from the permutations
%    - S0    : [Ma x Mb...] Observed values of the statistic
%    - nGoodA: [Ma x Mb...] Number of good samples for each set of measures in A
%    - nGoodB: [Ma x Mb...] Number of good samples for each set of measures in B
%    - PS    : [P x Ma x Mb... ] Matrix of permutation statistics (may be quite big!)

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
% 
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
% 
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Karim Jerbi, Karim N'Diaye, 2005-2006
%          Francois Tadel, 2016  (complete recoding)


% ===== PERMUTE INPUT =====
bst_progress('text', 'Initialization...');
% List of paired tests
isPaired = ismember(TestType, {'ttest_paired', 'wilcoxon_paired', 'signtest'});
% Put the dimension to permute in the 1st dimension
dimData = setdiff(1:ndims(A), dimPerm);
sizeData = size(A);
sizeData = sizeData(dimData);
A = permute(A, [dimPerm, dimData]);

    
function [pv,S0,nGoodA,nGoodB,PS] = bst_permtest(A, B, TestType, dimPerm, nPerm, tails, isZeroBad)
% PERMTEST: Generic randomization test.
%
% USAGE:  [pv,S0,nGoodA,nGoodB,PS] = bst_permtest(A, B, TestType, dimPerm, nPerm, tails, isZeroBad)
%
% INPUTS:
%    - A, B     : [Ma x Mb...] Variables to test   
%    - TestType : {'ttest_equal', 'ttest_unequal', 'ttest_paired', 'wilcoxon_paired', 'wilcoxon', 'signtest'}
%         ttest_equal   : Student's t-value, equal variance
%         ttest_unequal : Student's t-value, unequal variance
%         ttest_paired  : Student's t-value, paired data
%         signtest      : Sign of the differences (paired)
%         wilcoxon      : Signed ranks (paired)
%         absmean       : Difference of absolute values of means
%    - dimPerm   : Dimension to permute (ie. subjects)
%    - nPerm     : Number of permutations
%    - tails     : 'one+' (X1>X2), 'one-' (X1<X2), 'two' (X1<X2 or X1>X2)
%    - isZeroBad : If 1, excludes zeros from all the calculations
%
% OUTPUTS:
%    - pv    : [Ma x Mb...] p-values of the observed data, computed from the permutations
%    - S0    : [Ma x Mb...] Observed values of the statistic
%    - nGoodA: [Ma x Mb...] Number of good samples for each set of measures in A
%    - nGoodB: [Ma x Mb...] Number of good samples for each set of measures in B
%    - PS    : [P x Ma x Mb... ] Matrix of permutation statistics (may be quite big!)

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
% 
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
% 
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Karim Jerbi, Karim N'Diaye, 2005-2006
%          Francois Tadel, 2016  (complete recoding)


% ===== PERMUTE INPUT =====
bst_progress('text', 'Initialization...');
% List of paired tests
isPaired = ismember(TestType, {'ttest_paired', 'wilcoxon_paired', 'signtest'});
% Put the dimension to permute in the 1st dimension
dimData = setdiff(1:ndims(A), dimPerm);
sizeData = size(A);
sizeData = sizeData(dimData);
A = permute(A, [dimPerm, dimData]);
B = permute(B, [dimPerm, dimData]);
% Number of samples to permute
nA = size(A,1);
nB = size(B,1);
nAvgA = [];
nAvgB = [];
nGoodA = [];
nGoodB = [];
% Concatenate two input variables
X = cat(1, A, B);
% Try to compile the needed mex function
if (exist('bst_meanvar', 'file') ~= 3)
    if ~bst_compile_mex('toolbox/math/bst_meanvar', 0)
        error('Cannot compile mex function bst_meanvar.c...');
    end