Distribution of binary values
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi I have a random binary data set [1 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 1 1 0 1], I want to count how many duration of 1 and 0 are there. Answer will be [3 5 2 1 2 2 1] and [2 4 1 1 4 1] for 1 and 0 respectively..
0 Commenti
Risposte (1)
David Fletcher
il 29 Mar 2018
Modificato: David Fletcher
il 29 Mar 2018
test=[1 1 1 0 0 1 1 1 1 1 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 1 1 0 1]
out=regexp(char(test+48),'[0]+|[1]+','match')
start=test(1)
sz=length(out)
if logical(start)
type1=cellfun(@length,out(1:2:sz))
type0=cellfun(@length,out(2:2:sz))
else
type0=cellfun(@length,out(1:2:sz))
type1=cellfun(@length,out(2:2:sz))
end
type1 =
3 5 2 1 2 2 1
type0 =
2 4 1 1 4 1
Not bothered to add any error handling, but I'm sure you can amend it according to your needs
4 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings 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!