Length of the longest continuous string of the same number
조회 수: 24 (최근 30일)
이전 댓글 표시
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.
채택된 답변
Matt Fig
2011년 5월 27일
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
추가 답변 (5개)
Teja Muppirala
2011년 5월 27일
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
John D'Errico
2011년 5월 27일
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
Oleg Komarov
2011년 5월 27일
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
Andrew Murray
2011년 7월 9일
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
Nuchto
2013년 2월 1일
How to do this but for the longest consecutive sequence (1, 2, 3, 4, 5, 10, 14: here n = 5). Thanks!
댓글 수: 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!