DIfferentiating repetitions in a repeated sequence

조회 수: 1 (최근 30일)
Timothee Maniquet
Timothee Maniquet 2020년 6월 6일
댓글: Ameer Hamza 2020년 6월 8일
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!

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 6일
편집: Ameer Hamza 2020년 6월 7일
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
  댓글 수: 12
Timothee Maniquet
Timothee Maniquet 2020년 6월 8일
It worked! Thank you so much for your help.
Ameer Hamza
Ameer Hamza 2020년 6월 8일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by