Finding repeating pairs of specific numbers within column

2 次查看(过去 30 天)
I'm stuck on this probably very trivial problem:
I have a matrix like this (just much larger!):
A = [1 2 4 10 11 12 14 17 19; 5 30 30 50 90 70 5 30 5]'
A =
1 5
2 30 <--
4 30
10 50
11 90
12 70
14 5
17 30 <--
19 5
I'm now looking for "pairs" of specific numbers in A(:, 2). For example, I'd like to find all 30s that are preceded by 5s in A(:, 2) and extract the corresponding row in A(:, 1).
So I'd like to end up, in this example, with B = [2, 17];
How can I do that?
  1 个评论
Phillip
Phillip 2021-3-19
Vicky from stackoverflow solved this nicely - Thank you!!:
num_wanted=30;
num_previous=5;
is_match=[ false; A(1:end-1,2)==num_previous & A(2:end,2)==num_wanted ]; %the first element cannot be a match
A(is_match,1)

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by