hi ı wanna ask question about find a 1's number in array. for example the array should be like that 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 and ı only want to find a group of 1's. In array 0 1 0 or 1 0 1 or 0 0 0 1 0 something like that is an error in array. How can we do that. thanks

댓글 수: 2

Jan
Jan 2015년 10월 1일
What is the desired output for the given data? How should these "errors" be treated?
ali
ali 2015년 10월 1일
for example the desired output is for 1110101110101111 must be 3. But like 0 1 0 1 0 1 is a wrong.

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

답변 (5개)

Guillaume
Guillaume 2015년 10월 1일

0 개 추천

If you mean you want to find the start of the sequences of at least two consecutive 1, then it's simple using diff. You'll find the start of a sequence when the diff with the previous value is 1 (transition from 0 to 1), and you'll know it's followed by a 1 when the diff with the next value is 0, so:
A = [1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1]
startseq1 = find(A & diff([0 A]) == 1 & diff([A 0]) == 0)

댓글 수: 4

ali
ali 2015년 10월 1일
ı think this is the better one.
Guillaume
Guillaume 2015년 10월 1일
편집: Guillaume 2015년 10월 1일
Note that if you want the find the end of the groups and extract them, it's pretty much the same:
A = [1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1];
startseq1 = find(A & diff([0 A]) == 1 & diff([A 0]) == 0); %is 1, preceded by 0, followed by another 1
endseq1 = find(A & diff([A 0]) == -1 & diff([0 A]) == 0); %is 1, followed by 0, preceded by 1
sequences = arrayfun(@(s,e) A(s:e), startseq1, endseq1, 'UniformOutput', false)
ali
ali 2015년 10월 1일
but first one startseq1 = find(A & diff([0 A]) == 1 & diff([A 0]) == 0) do not work exactly.
Guillaume
Guillaume 2015년 10월 1일
I have no idea what you mean. The code I've written work:
>>A = [1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1];
>>startseq1 = find(A & diff([0 A]) == 1 & diff([A 0]) == 0)
startseq1 =
1 7 13
>>endseq1 = find(A & diff([A 0]) == -1 & diff([0 A]) == 0)
endseq1 =
3 9 16
>>sequences = arrayfun(@(s,e) A(s:e), startseq1, endseq1, 'UniformOutput', false);
>>celldisp(sequences)
sequences{1} =
1 1 1
sequences{2} =
1 1 1
sequences{3} =
1 1 1 1

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

Thorsten
Thorsten 2015년 10월 1일

0 개 추천

It is not entirely clear what you want to achieve; if you just want to get rid of the 0's, use
x = [1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1];
y = x(x==1);

댓글 수: 1

ali
ali 2015년 10월 1일
no for example in your array [1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1] the number of 1's group should be 3 (1 1 1)(1 1 1)(1 1 1 1) in array (0 1 0 and 0 1 0) is not a group of 1's because ıt is not all of 1's.

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

Andrei Bobrov
Andrei Bobrov 2015년 10월 1일

0 개 추천

[ii,~,~,v] = regexp(sprintf('%d',x(:)'),'1(1+)')
Jan
Jan 2015년 10월 1일

0 개 추천

Download and compile: Fex: RunLength . Then:
data = [1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1];
[b, n] = RunLength(data);
result = sum(b == 1 & n > 1);
If the data are small (e.g. just some kB), or you do not have a compiler installed already, use RunLength_M instead from the same submission.
ali
ali 2015년 10월 2일

0 개 추천

thanks all for great attention but ı think ı just write something wrong. For example the data 11101111001110001111110100000000011111111 is like that and there only two groups of 1 could be because number of 0 maybe to few then we should accept them as a 1 how can we do that we have to find group as a 2 there.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

질문:

ali
2015년 10월 1일

답변:

ali
2015년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by