Index a series of integers in a column of matrix

1 Ansicht (letzte 30 Tage)
Cheyenne Contreras
Cheyenne Contreras am 20 Aug. 2019
Kommentiert: Star Strider am 9 Sep. 2019
I am trying to find a function that will produce what row a series of numbers are together. For example in a column of the following:
T2 = [2 7 4 5 1 0 1 3 1 5 6 7 4 7 2 7]'
I would like to find which row there is a specific transition between 2 and 7 and vice versa. I have used the find(T2 == 2 & 7) function, however, that produces the location of every single location where the integer is exactly equal to 2 and 7.
Any suggestions?? Thank you!

Akzeptierte Antwort

Star Strider
Star Strider am 20 Aug. 2019
I am not sure what you want.
Try this:
T2 = [2 7 4 5 1 0 1 3 1 5 6 7 4 7 2 7]';
Idx = strfind(T2', [2 7])
Out = T2([Idx(:) Idx(:)+1])
producing:
Idx =
1 15
Out =
2 7
2 7
  4 Kommentare
Cheyenne Contreras
Cheyenne Contreras am 9 Sep. 2019
# Node
185 7
185 2
185 0
185 2
185 7
Hello,
Extending on this line of code, I need to determine the location of of 2 OR 7 when they are next to each other in the string. In the previous code, it was finding the occurance of the first location when 2 was before 7 and vice versa. Does anyone know if there is code to determine the second location regardless of order?
Thank you!
Star Strider
Star Strider am 9 Sep. 2019
A slight change in the earlier code:
T2 = [2 7 4 5 1 0 1 3 1 5 6 7 4 7 2 7]';
Idx1 = strfind(T2', [2 7]);
Idx2 = strfind(T2', [7 2]);
Idx = [Idx1(:); Idx2(:)]
Out = T2([Idx(:) Idx(:)+1])
produces:
Idx =
1
15
14
Out =
2 7
2 7
7 2
Note that you can use ‘Idx1’ and ‘Idx2’ separately, if you want specifically to index ‘[2 7]’ and ‘[7 2]’. I combined them here for convenience.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Time Series Events finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by