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!