Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.3 kB
3
Indexable
letters= ['A':'Z'];

%Assigning table properties
sz= [20 4];
varTypes = ["string","string","string","string"];
varNames = ["A","B","C","D"];
arrange_options= table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);

sz= [20 4];
var_types1 = ["string","string","string","string"];
var_names1 = ["First","Second","Third","Fourth"];
letter_seq= table('Size',sz,'VariableTypes',var_types1,'VariableNames',var_names1);

for n=1:20
%Selecting 4 random indexes 
%Converting indexes to letter
    letter_Idx  = randperm(length(letters),4);    
    selected_letters = letters(letter_Idx);               

%Sorting the indexes in ascending and descending order
    asc_ind= sort(letter_Idx);
    des_idx= sort(letter_Idx,'descend');

%Compare the random sequence with ascending and descending
%Create a function "comp" that comapres each value of the array with another
%Returns 1 if the two arrays are not equal
    while true
        rand_seq_1= letter_Idx(randperm(numel(letter_Idx),4));
        o1= comp (rand_seq_1,asc_ind);
        o2= comp (rand_seq_1,des_idx);
        if o1+o2==2                                       
            break
        end
    end
%Use the above function ("comp") to compare random sequence to ascending,
%descending and previous sequence
    while true
        rand_seq_2= letter_Idx(randperm(numel(letter_Idx),4));
        o1= comp (rand_seq_2,asc_ind);
        o2= comp (rand_seq_2,des_idx);
        o3= comp (rand_seq_2,rand_seq_1);
        if o1+o2+o3==3
            break
        end
    end

%Splitting the randomly generated letters array to individual letters
letter1= selected_letters(1);
letter2= selected_letters(2);
letter3= selected_letters(3);
letter4= selected_letters(4);

%Randomise the letter sequence
characters= {letter1,letter2,letter3,letter4};         
letter_seq(n,:)= characters(randperm(length(characters),4));       


%Coverting the Letters indexes to letters
%Generate four option sequences
option_1= letters(asc_ind);
option_2= letters(des_idx); 

option_3= letters(rand_seq_1);
option_4= letters(rand_seq_2);

options= {option_1,option_2,option_3,option_4};
arrange_options(n,:)= options(randperm(numel(options),4));
end

%Tranfering the data to excel file
writetable(letter_seq,'letter_task.xlsx');
writetable(arrange_options,'letter_task.xlsx', 'Sheet', 1, 'Range','E1');