Finding repeating pairs of specific numbers within column

2 vues (au cours des 30 derniers jours)
Phillip
Phillip le 18 Mar 2021
Commenté : Phillip le 19 Mar 2021
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 commentaire
Phillip
Phillip le 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)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by