Filter löschen
Filter löschen

Finding repeating pairs of specific numbers within column

2 Ansichten (letzte 30 Tage)
Phillip
Phillip am 18 Mär. 2021
Kommentiert: Phillip am 19 Mär. 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 Kommentar
Phillip
Phillip am 19 Mär. 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)

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by