Info
This question is closed. Reopen it to edit or answer.
How can I extract values from a second column after three repeating values in the first column?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Guillaume
on 5 Nov 2019
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 Comments
Guillaume
on 5 Nov 2019
Then
var2 = y(startruns([false, x(startruns) == 0 & runlengths >= 3]))
this will error if x ends in a run of 3 or more 0s.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!