Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
How can I extract values from a second column after three repeating values in the first column?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Guillaume
am 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]).
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!