Finding strings using an index - MATLAB -
i have character array list , wish tally number of substring occurrences against index held in numerical vector chr:
list = ccnncccnnncnncn chr = 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
ordinarily, searching adjacent string pairs i.e. 'nn' , utilise method:
count(:,1) = accumarray(chr(intersect([strfind(list,'cc')],find(~diff(chr)))),1);
using ~diff(chr) ensure pattern matching not cross index boundaries.
however, want match single letter strings i.e. 'n' - how can accomplish this? above method means last letter in each index missed , not counted.
the desired result above example 2 column matrix detailing number of 'c's , 'n's within each index:
c n 2 2 5 6
i.e. there 2c's , 2n's within index '1' (stored in chr
) - count restarts 0 next '2' - there 5c's , 6n's.
[u, ~, v] = unique(list); %// unique labels list in variable v result = full(sparse(chr, v, 1)); %// accumulate combinations of chr , v
this works arbitrary number of letters in list
, arbitrary number of indices in chr
, , chr
not sorted.
in example
list = 'ccnncccnnncnncn'; chr = [1 1 1 1 2 2 2 2 2 2 2 2 2 2 2].';
which produces
result = 2 2 5 6
the letter associated each column of result
given u
:
u = cn
Comments
Post a Comment