I want to count the number of 0s in a binary sequence which occurs for n number of times, where n=1:25.

조회 수: 27 (최근 30일)
Dwijaraj 2024년 5월 9일
댓글: Matlab Pro 2024년 6월 4일 9:57
Suppose I have a binary sequence A=[0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1]
Then I want to count in how many cases 0 occurs consecutively for n number of times preceded and succeeded by 1.
Here the desired output would be-
for n=1- noofzeros=1
n=2 - noofzeros=1
n=3- noof zeros=2
n=4- no of zeros=0
n=5- noofzeros=1
and so on....
댓글 수: 2없음 표시없음 숨기기
sai charan sampara 2024년 5월 9일
Hello Dwijraj, For n = 3 is the count 1 or 2 ? There is only one "10001" in the array A. Do you want to also count "0001" in the starting as a case of n=3?
Naren 2024년 5월 9일
I guess so, you should consider it as a circular array.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst 2024년 5월 10일
If you have the Image Processing Toolbox you can use a few functions in there:
% Define sample data.
A = [0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1]
A = 1x19
0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Put a row of zeros on top and bottom so we can use imclearborder
% to remove runs that don't have a 1 on both ends of the run.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% Get rid of runs that touch the border.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% Get the run lengths of each stretch of zeros into one vector.
allRunLengths = [props.Area]
allRunLengths = 1x4
2 1 5 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% PixelIdxList is the indexes that are in each run.
% If you want the starting index of each run of zeros,
% use the PixelIdxList and take the first index.
startingIndexes = zeros(1, numel(props)); % Preallocate
for k = 1 : numel(props)
startingIndexes(k) = props(k).PixelIdxList(1);
end
% Display it in the command window
startingIndexes
startingIndexes = 1x4
5 8 10 16
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

sai charan sampara 2024년 5월 9일
Hello Dwijraj,
You can use "find" to locate the positions of "1" in the array and then the difference in position of 2 consecutive "1"s is the number of continuous zeros. Then number of cases for each value of "n" can be counted. If we consider the array to be circular one more case is to be counted . It can be done similar to the code shown below:
A=[0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1];
idx=find(A,length(A))
idx = 1x5
4 7 9 15 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
arr=diff(idx)-1
arr = 1x4
2 1 5 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
arr=[arr,length(A)-idx(end)+idx(1)-1] % Extra case when we consider the array to be circular
arr = 1x5
2 1 5 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
no_of_zeros=zeros(25,1);
for i=1:length(arr)
no_of_zeros(arr(i))=no_of_zeros(arr(i))+1;
end
no_of_zeros
no_of_zeros = 25x1
1 1 2 0 1 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
sai charan sampara 2024년 5월 10일
You can add a variable "n" that can store the value of required array size and then define the size of the array "no_of_zeros" in terms of this variable "n". It can be done similar to the code below:
n=100;
A=randi([0,1],[1,50]);
idx=find(A,length(A))
idx = 1x23
1 3 6 7 8 14 22 25 27 28 29 30 31 34 35 36 37 38 40 41 42 46 49
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
arr=diff(idx)-1
arr = 1x22
1 2 0 0 5 7 2 1 0 0 0 0 2 0 0 0 0 1 0 0 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
arr=[arr,length(A)-idx(end)+idx(1)-1] % Extra case when we consider the array to be circular
arr = 1x23
1 2 0 0 5 7 2 1 0 0 0 0 2 0 0 0 0 1 0 0 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
no_of_zeros=zeros(n,1);
for i=1:length(arr)
if(arr(i)>0)
no_of_zeros(arr(i))=no_of_zeros(arr(i))+1;
end
end
no_of_zeros
no_of_zeros = 100x1
4 4 1 0 1 0 1 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Matlab Pro 2024년 6월 4일 9:57
Here is a solution without Image Processing TBX
The result is a histogram with bins 1:25 with occurancies
x=[0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 1 0 1 0 1 0 0 0 ];
x = randi([0, 1],[1,300]);
idx1 = x;
idx2 = [idx1(2:end), true];
f1 = find(idx1);
f2 = find(idx2);
f2 = f2(2:end);
if length(f1) > length(f2) % x starts with '1'
f2 = [f1(2)-1, f2]; % Missing 1s section's end
end
d = diff([f1;f2]);
[s,n] = hist(d,1:25)
s = 1x25
113 19 7 2 4 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
n = 1x25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by