Matlab - how to analyse sequences of binary behavioural responses? -


i interested in effect of previous responses on current response in behavioural test. example if on 3 previous trials in test participant answers 'no', 'yes', 'yes' likelihood of 'yes' response on current trial different if had answered 'yes', 'yes', 'no', , on.

to analyse need find instances of particular sequences of responses in dataset. e.g. every time yes yes yes occurs, every time yes yes no occurs..., , forth possible permutations of yes/no sequences.

to can hard code long chain of if/else statements in matlab (to work on fixed number of previous trials), or can write each possible sequence out , search both methods slow write.

rather code hand fixed number of previous trials, i.e. previous 3 responses, there sensible solution use instead previous n trials? i.e. want analyse, say, previous 5 trials, rather previous 3, chain of if/else statements required becomes unbearable!

nb. response data binary (i.e. left vs right, or yes vs no, etc.).

many in advance.

edit following illustration might clarify. quick example runs through each response in sequence and, if current response 'y', counts number of instances of each permutation of yes/no in previous 2 trials.

mysequencedata = {'y' 'y' 'y' 'n' 'n' 'y' 'n' 'n' 'y' 'y' 'n' 'y'}; numprevioustrials = 2;  yycount = 0; yncount = 0; nycount = 0; nncount = 0;  = numprevioustrials+1:length(mysequencedata)     currenttrial = mysequencedata(i);     if strcmp(mysequencedata(i), 'y')         if strcmp(mysequencedata(i-1), 'y')             if strcmp(mysequencedata(i-2), 'y')                 yycount = yycount+1;             elseif strcmp(mysequencedata(i-2), 'n')                 nycount = nycount+1;              end         elseif strcmp(mysequencedata(i-1), 'n')             if strcmp(mysequencedata(i-2), 'y')                 yncount = yncount+1;             elseif strcmp(mysequencedata(i-2), 'n')                 nncount = nncount+1;              end         end     end                        end 

if want count y/n permutations in previous 3 trials, or 5 trials, , on, have re-write all, , using if/else statements becomes unworkable.

my problem in trying find solution can write n trials outset, rather specific number of previous trials.

thanks again.

it should work you:

mysequencedata = [1 1 1 0 0 1 0 0 0 0 0 0];  n = length(mysequencedata); j = 1:n-1  n = j; a1 = [0:2^n-1]'; %' a2{j} = rem(floor(a1*pow2(-(n-1):0)),2); = n:length(mysequencedata) short_seq =  mysequencedata(i-n+1:i);    out{i-n+1} = ismember(a2{j},short_seq,'rows');                      end counting      = cellfun(@find,out,repmat({1},1,length(out))); counting_n{j} = histc(counting,[1:length(a2{j})]); end 

the matrix a2 list possible binary combination. matrix counting_n count number of time combination appears.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -