given the following cell array:
strings = {'str1', 'str2', 'str2', 'str1', 'str2', 'str2', 'str1', 'str1', 'str2'};
I want to find the onsets and offsets (first occurrence and last occurrence) of a particular value. For example, the following are the onsets and offsets for ‘str1’:
onset_str1 = [1, 4, 7, ];
offset_str1 = [1, 4, 8, ];
And here are the on- and offsets for ‘str2’:
onset_str2 = [2, 5, 9];
offset_str2 = [3, 6, 9];
Currently I do something like this:
[blub, ia, ic] = unique(strings, 'first');
all_str1 = find(ic == 1); % 1 4 7 8
all_str2 = find(ic == 2); % 2 3 5 6 9
Using all_str1 and all_str2 I would then look for consecutive values (using diff for example) and determine that way the on and offsets. However this kind of implementation feels ‘hackish’ to me.
What other ways are there to extract the on and offsets in my sequence efficiently?
ok, but next up, just use logicals and find to find the rising/falling edges:
or strfind if that’s more clear to understand to you: