필터 지우기
필터 지우기

Uninterrupted segment length?

조회 수: 2 (최근 30일)
Gavin Rumbaugh
Gavin Rumbaugh 2016년 11월 28일
댓글: Qisq 2018년 4월 27일
Let's say I have an array indexes:
Indexes = [1122221122111211112222222]
How do I get the length of uninterrupted 2s? I want to output to be
output=[ 4 2 1 7]

채택된 답변

bio lim
bio lim 2016년 11월 28일
For a number that has longer digits, I am not sure. However, if it is within acceptable number of digits
format long
Indexes = [112222112211121];
newIndexes=str2double(regexp(num2str(Indexes),'\d','match'))
coffee = find(diff([-1 newIndexes -1]) ~= 0);
length = diff(coffee);
output = length(1+(newIndexes(1)==1):2:end);
The output is:
output =
4 2 1

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 11월 28일
편집: Roger Stafford 2016년 11월 28일
f = find(diff([false,Indexes==2,false])~=0);
output = f(2:2:length(f))-f(1:2:length(f));
  댓글 수: 1
Qisq
Qisq 2018년 4월 27일
Very elegant solution. Thanks.

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

Community Treasure Hunt

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

Start Hunting!

Translated by