find all sequences between delimiters in an array

3 visualizaciones (últimos 30 días)
Kais
Kais el 11 de Mayo de 2021
Editada: Stephen23 el 11 de Mayo de 2021
Given a sequence of numbers and a start and an end delimiters, how to find the index (start and end) of all possible sequence of numbers between delimiters (sequences can be of any length). Example:
v = [1 1 0 2 0 2 1 2 1 1 1 0 2 1 2 0 0 1 1 1 1 1 1 2 1 0 ]
A list of non-overalpping sequences and their start and end index that begin with 2 and end with 0 would be:
[2 0]
[2 1 2 1 1 1 0]
[2 1 2 0]
[2 1 0]
I feel like this could be solved using regular expression.
  2 comentarios
Stephen23
Stephen23 el 11 de Mayo de 2021
If the numbers are always positive integers <10 then this is easy. If the values can be >=10 or if you want to consider non-integer values then the task requires a bit more thought. What restrictions/limits do you have on the number values?
Kais
Kais el 11 de Mayo de 2021
I can live with the range of numbers in the example. Three values!

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 11 de Mayo de 2021
Editada: Stephen23 el 11 de Mayo de 2021
v = [1,1,0,2,0,2,1,2,1,1,1,0,2,1,2,0,0,1,1,1,1,1,1,2,1,0];
[begIdx,endIdx,~,match] = regexp(sprintf('%d',v),'2.*?0')
begIdx = 1×4
4 6 13 24
endIdx = 1×4
5 12 16 26
match = 1×4 cell array
{'20'} {'2121110'} {'2120'} {'210'}

Más respuestas (1)

Jonas
Jonas el 11 de Mayo de 2021
you could convert the array to a string or char array and then use extractBetween() which does what you want
  1 comentario
Kais
Kais el 11 de Mayo de 2021
Thanks! it does not unfortunately. It will return overlapping sequences and it does not return indices.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by