Finding repeating pairs of specific numbers within column

2 views (last 30 days)
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 Comment
Phillip
Phillip on 19 Mar 2021
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)

Sign in to comment.

Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!