function permute_data( np, data, nsamples, samesvm, paths, def)

% retrieve some relevant variables
ntestex     =   length( data.labels);
nmaps       =   ntestex/2;
npos        =   [1:ntestex; [nmaps+1:ntestex, 1:nmaps]];
n_tests     =   sum( samesvm==samesvm(:,np));
ranknorm    =   n_tests.*((1:ntestex)*data.labels);
nSubjects   =   size( data.dist, 1);
 
% preallocate matrix with all data
d_data.dist    =  NaN( [size( data.dist) nsamples]);

% iterate over n samples to create shuffled data & base metrics
for ns = 1:nsamples
    
    % iterate over subjects and classification pairs
    for ip = 1:nSubjects
        
        % randomly determine: swap test label or not
        yy              =   repmat( logical( randi( 2, 1, nmaps)-1), 1, 2);
        yy(2,:)         =   ~yy;
        
        % retrieve distance to bound with swapped labels
        d_data.dist(ip,:,:,ns)  =   data.dist(ip, :, npos( logical( yy)));
        
    end % end of subject loop
end % end of n samples loop

saveit  =   [];

% add observed data to position 1 (raw distance data)
d_data.dist  =   cat( 4, data.dist, d_data.dist);
if isempty( def) || any( contains( def.metrics, {'acc'}))
    saveit = sprintf('%s,"%s"', saveit,'d_data');
end

% create z-scored data
if isempty( def) || any( contains( def.metrics, {'zdb','mfc'})) 
    z_data.dist  =   zscore( d_data.dist, 0, 3);
    saveit = sprintf('%s,"%s"', saveit,'z_data');
end

% create forced classification data (subtract paired labels)
if isempty(def) || any( contains( def.metrics, 'pfc')) 
    f_data.dist  =   d_data.dist(:,:,data.labels==1,:) - d_data.dist(:,:,data.labels==-1,:);
    saveit = sprintf('%s,"%s"', saveit,'f_data');
end

% create rank-order data
if isempty(def) || any( contains( def.metrics, 'rdb')) 
    [~,tempranks]=  sort( d_data.dist, 3);
    r_data.dist  =  tempranks ./ ranknorm; %#ok<*STRNU>
    saveit = sprintf('%s,"%s"', saveit,'r_data');
end

% save all data (and delete from workspace to save memory)
tempath = fullfile( paths.res, 'temp', sprintf( '%s_np%g.mat', paths.temp, np)); %#ok<NASGU>
eval( sprintf( 'save( tempath %s, "-v7.3");', saveit));

clearvars d_data z_data f_data r_data

end