Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
How can I count the largest number of repeated numbers in a double?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a double that is 5644x1 and it consists of nothing but 0's and 1's.
ex: 00000000101100000011110000110000
I want to count the longest length of repeated 0's in this double, which in the case of this example would be 8. Can you please help?
댓글 수: 0
답변 (3개)
John D'Errico
2019년 9월 13일
편집: John D'Errico
2019년 9월 13일
First, this is NOT a double vector.
00000000101100000011110000110000
It might be a string. But if you tried to write that vector as a double, it would be just one number, in decimal form. So you might have it stored as a vector binary elements. I'll assume you have a character string, but the answer is trivially changed if not.
V = '00000000101100000011110000110000';
We are looking for the longest length of repeated 0's.
st10 = strfind(['1',V],'10') + 1;
st01 = strfind([V,'1'],'01');
st01 - st10 + 2
ans =
8 1 6 4 4
So the length of the LONGEST such string of zeros is just...
max(st01 - st10 + 2)
ans =
8
If your vector is actually a numeric vector, perhaps like this, strfind still works
V=[0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0];
st10 = strfind([1,V],[1 0]) + 1;
st01 = strfind([V,1],[0 1]);
max(st01 - st10 + 2)
ans =
8
댓글 수: 5
John D'Errico
2019년 9월 14일
I guess it is one of those features that date back to the dark ages of MATLAB, when there was sort of a duality beween strings and numeric integer vectors containing ASCII code values. The reason why you can still do things like this:
C = 'ABCDE';
+C % ascii code values
ans =
65 66 67 68 69
C - 'A'
ans =
0 1 2 3 4
N = '012345';
N - '0'
ans =
0 1 2 3 4 5
Adam Danz
2019년 9월 14일
char([73 39 108 108 32 104 97 118 101 32 102 117 110 32 119 105 116 104 32 116 104 105 115 33])
Image Analyst
2019년 9월 13일
You can use regionprops(), if you have the Image Processing Toolbox
signal = [0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0];
props = regionprops(logical(signal == 0), 'Area'); % Measure lengths of each run of zeros
longestStretch = max([props.Area]) % Find the length of the longest one.
You get:
longestStretch =
8
댓글 수: 1
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!