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)
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];

Antworten (1)

Guillaume
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]).
  2 Kommentare
SW
SW am 5 Nov. 2019
Sorry for the typo in question. I want the var2 output to read [17,29]. It should only extract the y variable after at least 3 repeating zeros, so should not read the y=1. Do you know how to correct?
Guillaume
Guillaume am 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.

Community Treasure Hunt

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

Start Hunting!

Translated by