Length of the longest continuous string of the same number
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Respuesta aceptada
Matt Fig
el 27 de Mayo de 2011
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.
0 comentarios
Más respuestas (5)
Teja Muppirala
el 27 de Mayo de 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;
else
if len > lenmax
lenmax = len;
end
len = 1;
end
end
0 comentarios
John D'Errico
el 27 de Mayo de 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.
0 comentarios
Oleg Komarov
el 27 de Mayo de 2011
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.
0 comentarios
Andrew Murray
el 9 de Jul. de 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
0 comentarios
Nuchto
el 1 de Feb. de 2013
How to do this but for the longest consecutive sequence (1, 2, 3, 4, 5, 10, 14: here n = 5). Thanks!
1 comentario
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!