Find repeated expression in array of strings, return logical.
Mostrar comentarios más antiguos
I have data of the type
looking_for = ["apple", "melon"]
in
my_data = ["The apple is red", "The bee was yellow", "I am eating a melon", "The melon is sweet"]
with
timing = [2.5, 5, 10, 18]
I want to find when a regular expression was repeated consecutively and then return a logical index that pertains to the first observation of the repetition.
My approach:
1) Find out if the string contains one of the regular expression in looking_for, e.g. melon. I solve this using
idx = cellfun(@(x)( ~isempty(x) ), regexp(my_data, "apple"));
2) Then i transpose and multiply my indexing with the timing to get the relevant timings & remove the zeros (not shown here)
apple_timing = transpose(idx).*timing;
Which would give me a cell called apple_timing with a value of 2.5, which is exactly what I want.
I would like a bit of code that returns a variable called repeat_timing. In the case of the melon, this would return 18 - the first observed consecutive repeat of the regular expression melon.
1 comentario
Jos (10584)
el 22 de Dic. de 2017
huh, I don't see apple being repeated in your strings?
And why do you use cellfun and regexp rather than the dedicated string find function CONTAINS which returns a logical array directly?
contains(my_data, looking_for) % → [1 0 1 1]
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!