function [trialmatrix] = sd_trialmatrix(cfg,Gparams)

% set up the parts of the matrix
trials              = cfg.trials;
orientation_base    = Gparams.stim.sd_orientation;

h_location          = [zeros(1,trials+1)];
stim_orientation    = [-orientation_base*ones(1,trials/4), ...
                       zeros(1,trials/2+1), ...
                       orientation_base*ones(1,trials/4)];
button_mapping      = [ones(1,trials/8), 2*ones(1,trials/8), ...
                       ones(1,trials/4), 2*ones(1,trials/4), round(rand)+1, ...
                       ones(1,trials/8), 2*ones(1,trials/8)];

% join the parts of the matrix
% 1st row: horizontal location 
% 2nd row: orientation of stimulus
% 3rd row: button mapping
trialmatrix            = [h_location; stim_orientation; button_mapping];

% establish frequency criteria based on the proportions of stimuli that are
% oriented left, vertical, and right
frequencycriteria = 100 * [Gparams.stim.sd_prop_left*Gparams.stim.sd_prop_left; ...
    Gparams.stim.sd_prop_left*Gparams.stim.sd_prop_vertical; ...
    Gparams.stim.sd_prop_left*Gparams.stim.sd_prop_right; ...
    Gparams.stim.sd_prop_vertical*Gparams.stim.sd_prop_left; ...
    Gparams.stim.sd_prop_vertical*Gparams.stim.sd_prop_vertical; ...
    Gparams.stim.sd_prop_vertical*Gparams.stim.sd_prop_right; ...    
    Gparams.stim.sd_prop_right*Gparams.stim.sd_prop_left; ...
    Gparams.stim.sd_prop_right*Gparams.stim.sd_prop_vertical; ...
    Gparams.stim.sd_prop_right*Gparams.stim.sd_prop_right];

correctcombination = 0;

while correctcombination == 0
    
    combination = [];
    
    for n = 1:size(trialmatrix,2)-1
        if trialmatrix(2,n) == -orientation_base && trialmatrix(2,n+1) == -orientation_base;
        combination = [combination,1];
        elseif trialmatrix(2,n) == -orientation_base && trialmatrix(2,n+1) == 0;
        combination = [combination,2];
        elseif trialmatrix(2,n) == -orientation_base && trialmatrix(2,n+1) == orientation_base;
        combination = [combination,3];
        elseif trialmatrix(2,n) == 0 && trialmatrix(2,n+1) == -orientation_base;
        combination = [combination,4];
        elseif trialmatrix(2,n) == 0 && trialmatrix(2,n+1) == 0;
        combination = [combination,5];
        elseif trialmatrix(2,n) == 0 && trialmatrix(2,n+1) == orientation_base;
        combination = [combination,6];
        elseif trialmatrix(2,n) == orientation_base && trialmatrix(2,n+1) == -orientation_base;
        combination = [combination,7];
        elseif trialmatrix(2,n) == orientation_base && trialmatrix(2,n+1) == 0;
        combination = [combination,8];
        elseif trialmatrix(2,n) == orientation_base && trialmatrix(2,n+1) == orientation_base;
        combination = [combination,9];
        else 
        combination = [combination,0];
        end
    end

    percentages = tabulate(combination);

    if size(percentages,1) == size(frequencycriteria,1);
        if percentages(:,3) == frequencycriteria;
            correctcombination = 1;
        else
            trialmatrix = trialmatrix(:,randperm(size(trialmatrix,2)));
        end
    else
        trialmatrix = trialmatrix(:,randperm(size(trialmatrix,2)));
    end
  
end

end


