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..

답변 (1개)

David Fletcher
David Fletcher 2018년 3월 29일
편집: David Fletcher 2018년 3월 29일

0 개 추천

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

Amitrajit Mukherjee
Amitrajit Mukherjee 2018년 3월 29일
what is test+48 ??
David Fletcher
David Fletcher 2018년 3월 29일
It converts the numeric array to an array of chars for the purpose of using regexp. You could use mat2str instead if you wanted.
Amitrajit Mukherjee
Amitrajit Mukherjee 2018년 4월 2일
Can you please tell me what is the use of "char(test+48)" ?
Amitrajit Mukherjee
Amitrajit Mukherjee 2018년 4월 2일
Thank you so much..It works... :)

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2018년 3월 29일

댓글:

2018년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by