MATLAB Answers


Length of the longest continuous string of the same number

Asked by Matthew
on 27 May 2011

If i have a column vector A where A = [5;5;1;2;3;4;5;5;5;5;6;7;8] How can i find in general the length(=l) of the longest continuous string of the same number being repeated. So here in A it would be l=4...for the number 5.

  1 Comment

Sounds like homework. Is it?


No products are associated with this question.

6 Answers

Answer by Matt Fig
on 27 May 2011
 Accepted answer

Here is one way to do it.

A = [5;5;1;2;3;4;5;5;5;5;6;7;8].'; % Transpose to row vector.
[M,V] = regexp(sprintf('%i',[0 diff(A)==0]),'1+','match');
[M,I] = max(cellfun('length',M));
M = M + 1  % Holds the max length.
I = V(I) - 1  % Holds the starting index of first run of length M.
V = A(I)  % The value of the run.


Answer by Teja Muppirala
on 27 May 2011

A more explicit way of doing it:

lenmax = 1;
len = 1;
for n = 2:numel(A)
    if A(n) == A(n-1);
        len = len+1;
        if len > lenmax
            lenmax = len;
        len = 1;


Answer by Oleg Komarov
on 27 May 2011

You can use findseq

A = [5;5;1;2;3;4;5;5;5;5;6;7;8]
out = findseq(A);
out =
     5     1     2     2
     5     7    10     4

It tells you that there are two sequences of repeated 5. The first starts at position 1 and ends in pos 2 and it's length is 2. Same interpretation for the other sequence.


Answer by John D'Errico
on 27 May 2011

Well, what if you tried differencing the vector?

help diff

What happens to repeats? So now, you need only look for the length of the longest string of zeros. How can you do that?

Simplest seems to be to convert it to a logical vector, zeros and ones. So, something like...

diff(A) ~= 0

Now, how do you find strings of consecutive zeros?

help strfind

What happens if you look for the pattern [1 0]?

How about the pattern [0 1]?

Must these be interleaved? Note, you only need to worry about the end points. So perhaps see what happens when you do this...

[1,(diff(A) ~= 0),1]

Given this, you should be able to write the code yourself now. TRY IT!!!!!!! The way to learn MATLAB is by doing it.


Answer by Andrew Murray
on 9 Jul 2011

This can be done in one line:

out = max(diff(find([1,diff(A'),1])));

out = 4

This works by looking at the differences between successive values, which is zero within a run, which means that non-zero values mark the beginning and end of a run (Because you have to account for runs terminating at the beginning and end of the array you are querying, you have to append a 1 to the beginning and end of the difference array, hence the transposition of A in the command line above). By finding the difference between the indices that mark the beginning and end of a run, you get the lengths of the run. Basic idea from:

If you need to know the value of the number in the longest run, you need more code:

runs = diff(find([1,diff(A'),1]));

which gives you the lengths of all runs, and

starts = find([1,diff(A')]);

which gives you the index at which each run starts. With this done, this command gives you the value of the numbers in the longest run

top_value = A(starts(find(runs == max(runs))));

top_value = 5


Answer by Nuchto
on 1 Feb 2013

How to do this but for the longest consecutive sequence (1, 2, 3, 4, 5, 10, 14: here n = 5). Thanks!

  1 Comment

You should re-post the question, but if you diff() your vector of values and look at the output, you might realize that you are back in the frame of the original question (and you would be looking for sequences of zeros).

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!