findConS.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. function [dataOut,dataOutI] = findConS(dataIn,seq,nTrials,nConS,exactNConSI)
  2. % IDENTIFY consecutive standard responses
  3. % This function identifies consecutive standard responses in the oddball
  4. % paradigm. The number of responses is defined by nConS
  5. % define some variables
  6. pSmpl = size(dataIn,1);
  7. nBlcks = size(dataIn,3);
  8. nFiles = size(dataIn,4);
  9. nFilt = size(dataIn,5);
  10. dataOut = cell(nBlcks,nFiles); % preallocate
  11. dataOutI = cell(nBlcks,nFiles); % preallocate
  12. for f = 1:nFiles
  13. for b = 1:nBlcks
  14. % create std sequences
  15. seqStd = ~seq(1:nTrials(b,f),b,f)';
  16. % create vector that contains all clusters of consecutive standards
  17. % code from forum: replace ones by number of consecutive repetitions
  18. switch exactNConSI
  19. case 0
  20. vecClI = double(seqStd);
  21. case 1
  22. d = [1,diff(seqStd)~=0,1]; % TRUE if values change
  23. n = diff(find(d)); % Number of repetitions
  24. X = repelem(n,n);
  25. vecClI = X.*seqStd; % accept only clusters of consecutive standards and not deviants
  26. vecClI(vecClI<nConS) = 0; % replace numbers that are smaller than criterion (less than X consecutive repetitions) by 0
  27. vecClI(vecClI>=nConS) = 1; % replace numbers that are equal or larger than criterion (X consecutive repetitions or more) by 1
  28. end
  29. % count appearances of consecutive standards in each accepted
  30. % cluster (code from forum)
  31. B = vecClI;
  32. d2 = [true,diff(B)~=0];
  33. r = find(d2);
  34. B(~d2) = 1;
  35. B(d2) = [1,1-diff(r)];
  36. B(1) = 1;
  37. B = cumsum(B);
  38. % remove standards that appear in a cluster at a higher position
  39. % than nConS
  40. B(B<=nConS) = 1; % replace numbers that are equal or smaller than criterion by 1
  41. B(B>nConS) = 0; % replace numbers that are larger than criterion by 0
  42. vecClI = logical(vecClI.*B); % accept only the first X standard responses of clusters of consecutive standards that are equal or larger than X
  43. % identify number and size of accepted clusters
  44. bCl = diff([0,vecClI,0]); % vector that marks borders of clusters
  45. nCl = sum(bCl==1); % number of accepted clusters (1 marks the starting point of a cluster)
  46. sCl = find(bCl==1); % starting points of clusters
  47. eCl = find(bCl==-1)-1; % ending points of clusters
  48. lCl = eCl-sCl+1; % size of clusters
  49. % produce matrices that contain all accepted vectors
  50. dataOutI{b,f} = zeros(nConS,nCl); % preallocate
  51. dataOut{b,f} = zeros(pSmpl,nConS,nCl,nFilt); % preallocate
  52. for n = 1:nCl % once for each cluster
  53. dataOutI{b,f}(1:lCl(n),n) = vecClI(sCl(n):eCl(n)); % replace 0s by 1s for each std in the given cluster
  54. dataOut{b,f}(:,1:lCl(n),n,:) = dataIn(:,sCl(n):eCl(n),b,f,:); % create new variable that contains all std responses of the given cluster
  55. end
  56. dataOutI{b,f} = logical(dataOutI{b,f}); % convert to logical
  57. end
  58. end