Finding the # of sequences in an array

跟風遠走 提交于 2019-12-24 08:39:32

问题


I need to report the # of sequences in an array. For example:

A=[ 1 1 -1 -1 -1 -1 -1 0 1 -1 -1 -1 -1 1 1 -1 -1 1 0 1 1]

and I have to report the # of times a number comes consecutively, such as, one sequence of

5 -1s ([-1 -1 -1 -1 -1]) and one sequence of

4 -1s ([-1 -1 -1 -1]).

How can I find how many sequences of numbers there are?


回答1:


You may use run-length encoding to perform this task

function [rl data] = runLength( vec )
% run length encoding for vector vec
rl = ( find( vec ~= [vec(2:end), vec(end)+1] ) );
data = vec( rl );
rl(2:end) = rl(2:end) - rl(1:end-1);

Applying run-length encoding to A

>> [rl data] = runLength( A )
rl =
   [ 2 5 1 1 4 2 2 1 1 2 ]
data =
   [ 1 -1 0 1 -1 1 -1 1 0 1 ]

So, if you are interested in the number of sequences of length > n all you need is

>> nnz( rl > n )



回答2:


If you only have a few number of possible element values in A (as in the example in the question where there only are three values, -1, 0 and 1) you could loop through these and use the following few steps to get the lengths of the different sequences.

Here is an example checking A == -1:

A = [1 1 -1 -1 -1 -1 -1 0 1 -1 -1 -1 -1 1 1 -1 -1 1 0 1 1];
B = [0, A==-1, 0];

Use the diff() function to find the beginning and end of each sequence and subtract the two vector to get the sequence lengths.

>> C = find(diff(B)==-1)-find(diff(B)==1)

C =

     5     4     2

Here we can see that there is one sequence of length five, followed by one of length four and one of lenth two. We could also use histc() to get the frequency of these lengths in a vector.

>> D = histc(C,1:max(C))

D =

     0     1     0     1     1

Repeating the procedure with another value, for example checking B = [0, A==1, 0]; gives us:

C =

     2     1     2     1     2

D =

     2     3


来源:https://stackoverflow.com/questions/14809429/finding-the-of-sequences-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!