MATLAB: DIfferentiating repetitions in a repeated sequence

blocgroupsMATLAB and Simulink Student Suiterepeated sequencerepetitiontrial

Hi,
I am working with eye tracking data. I have a variable that represents trials, that is made of a repeated sequence. It looks like this: [1,1,1,1,…,2,2,2,2,…,3,3,3,3,3,…,4,4,4,…] etc. I put dots here to show that there are a lot of these numbers. The sequence repeates itself each time a new bloc started in the experiment, and I would like to be able to know which bloc a trial belongs to.
How could I, for example, add 1000 to the first repetition of the sequence, then 2000 to the second one, so that I would know, if I see [3034] that I have the 34th trial from the 3rd bloc ?
Thank you for your help!

Best Answer

Try this
x = [1 1 1 1 2 2 2 3 3 3 3 3 1 1 2 3 3 1 2 3];
idx = [1 find(diff(x)<0) + 1];
y = zeros(size(x));
y(idx) = 1;
y = cumsum(y);
res = splitapply(@(xo, yo) {splitapply(@(xi) {1000*yo(1) + xi(1)*ones(size(xi))}, xo, xo)}, x, y, y);
res = [res{:}];
res = [res{:}];
Result
>> res
res =
Columns 1 through 6
1001 1001 1001 1001 1002 1002
Columns 7 through 12
1002 1003 1003 1003 1003 1003
Columns 13 through 18
2001 2001 2002 2003 2003 3001
Columns 19 through 20
3002 3003