Filtering sequences in MATLAB -
is possible regular expressions matlab filter things out? i'm looking let me take vector like:
[1 2 1 1 1 2 1 3 3 3 3 1 1 4 4 4 1 1]
and return:
[3 3 3 3 4 4 4]
these uninterrupted sequences (where there's no interspersion).
is possible?
using regular expressions
use matlab's built-in regexp
function regular expression matching. however, have convert input array string first, , feed regexp
:
c = regexp(sprintf('%d ', x), '(.+ )(\1)+', 'match')
note separated values spaces regexp
can match multiple digit numbers well. convert result numerical array:
res = str2num([c{:}])
the dot (.
) in pattern string represents character. find sequences of digits only, specify them in brackets ([]
). instance, pattern find sequences of 3 , 4 be:
([34]+ )(\1)+
a simpler approach
you can filter out successively repeating values checking similarity between adjacent elements using diff
:
res = x((diff([nan; x(:)])' == 0) | (diff([x(:); nan])' == 0))
optionally, can keep values result, example:
res(res == 3 | res == 4)
Comments
Post a Comment