I want to count the number of 0s in a binary sequence which occurs for n number of times, where n=1:25.
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
sai charan sampara
il 9 Mag 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?
Risposta accettata
Image Analyst
il 10 Mag 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]
% 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))
% Get rid of runs that touch the border.
mask = imclearborder(mask)
props = regionprops(mask(2,:), 'Area', 'PixelIdxList');
% Get the run lengths of each stretch of zeros into one vector.
allRunLengths = [props.Area]
% 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
0 Commenti
Più risposte (1)
sai charan sampara
il 9 Mag 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))
arr=diff(idx)-1
arr=[arr,length(A)-idx(end)+idx(1)-1] % Extra case when we consider the array to be circular
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
3 Commenti
sai charan sampara
il 10 Mag 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))
arr=diff(idx)-1
arr=[arr,length(A)-idx(end)+idx(1)-1] % Extra case when we consider the array to be circular
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
Matlab Pro
il 4 Giu 2024
Hi @Dwijaraj
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)
Vedere anche
Categorie
Scopri di più su Sparse Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!