matlab - Finding the # of sequences in an array -
i need study # of sequences in array. example:
a=[ 1 1 -1 -1 -1 -1 -1 0 1 -1 -1 -1 -1 1 1 -1 -1 1 0 1 1]
and have study # of times number comes consecutively, such as, 1 sequence of
5 -1s ([-1 -1 -1 -1 -1])
, 1 sequence of
4 -1s ([-1 -1 -1 -1])
.
how can find how many sequences of numbers there are?
you may utilize run-length encoding perform task
function [rl data] = runlength( vec ) % run length encoding vector vec rl = ( find( vec ~= [vec(2:end), vec(end)+1] ) ); info = vec( rl ); rl(2:end) = rl(2:end) - rl(1:end-1);
applying run-length encoding a
>> [rl data] = runlength( ) rl = [ 2 5 1 1 4 2 2 1 1 2 ] info = [ 1 -1 0 1 -1 1 -1 1 0 1 ]
so, if interested in number of sequences of length > n
need is
>> nnz( rl > n )
matlab numbers sequences
No comments:
Post a Comment