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.
Best Answer