Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How can I extract values from a second column after three repeating values in the first column?
조회 수: 1 (최근 30일)
이전 댓글 표시
x = [1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1];
y = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29];
If x repeats 0 three or more times, extract the y value from the first repeating 0 into variable 1
If x reads 1 after at least three consecutive 0 values, extract the y value from the first repeating 1 into variable 2
Resulting in...
var1 = [5, 24];
var2 = [17, 29];
댓글 수: 0
답변 (1개)
Guillaume
2019년 11월 5일
One way:
x = [1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1];
y = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29];
startruns = find([NaN, diff(x)]); %find location of the start of the runs (when diff is not 0). Prepend by NaN so the first index is found.
runlengths = diff([startruns, numel(x)+1]); %length of runs is difference between consecutive starts
var1 = y(startruns(x(startruns) == 0 & runlengths >= 3))
var2 = y(startruns(x(startruns) == 1 & runlengths >= 3))
which shows that you got it wrong for your var2 by the way. (var2 is [1, 17]).
댓글 수: 2
Guillaume
2019년 11월 5일
Then
var2 = y(startruns([false, x(startruns) == 0 & runlengths >= 3]))
this will error if x ends in a run of 3 or more 0s.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!