- c is the column of your question
- the first column of ans are indicies of the first "one" in a group and the second column the last.
sum separate continuous values
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello Matlab Senior Users
I have a column of data 0 and 1 and the example data is like this format. There are 2 number of continous 1 value group. I don't want all groups of 1 values. I need only the number of continuous 1 value groups. It is 2 for this sample data (index 3 to 4 and index 6 to 8).
0
0
1
1
0
1
1
1
0
1
0
1
0
0
I tried some code like this suggested by some expert. But the answer is not correct.
ids=find(x==1);
choice=diff(ids)~=1;
ids([true;choice])
indexes=ids([true;choice]);
groups=numel(indexes)
If someone kindly advice me in for loop or while loop if will helpful for me.
Thank you very much for your helps.
댓글 수: 0
채택된 답변
per isakson
2020년 9월 28일
편집: per isakson
2020년 9월 28일
%%
d = diff( c );
ixb = find( d == +1 ) + 1;
ixe = find( d == -1 );
%%
is_group = ixe >= ixb + 1;
[ ixb(is_group), ixe(is_group) ]
outputs
ans =
3 4
6 8
>>
where
Test this script with other input columns.
댓글 수: 6
per isakson
2020년 9월 28일
%%
d = diff( value_index(:,2) );
ixb = find( d == +1 ) + 1;
ixe = find( d == -1 );
%%
if ixb(end) > ixe(end) % the last group includes the last row of value_index
ixe(end+1) = size( value_index, 1 );
end
%%
is_in_group = false( size(value_index,1), 1 );
for jj = 1 : numel( ixb )
is_in_group( ixb(jj) : ixe(jj) ) = true;
end
%%
max_value_in_groups = max( value_index( is_in_group, 1 ) );
where value_index is the table of your comment.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!