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

2 visualizaciones (últimos 30 días)
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 comentarios
sai charan sampara
sai charan sampara el 9 de Mayo de 2024
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?

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 10 de Mayo de 2024
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.
mask = logical(padarray(A == 0, 1, 0))
mask = 3x19 logical array
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.
mask = imclearborder(mask)
mask = 3x19 logical array
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
props = regionprops(mask(2,:), 'Area', 'PixelIdxList');
% 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>

Más respuestas (1)

sai charan sampara
sai charan sampara el 9 de Mayo de 2024
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 comentarios
sai charan sampara
sai charan sampara el 10 de Mayo de 2024
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
Matlab Pro el 4 de Jun. de 2024
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>

Iniciar sesión para comentar.

Categorías

Más información sobre Sparse Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by