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.

 採用された回答

per isakson
per isakson 2020 年 9 月 28 日
編集済み: per isakson 2020 年 9 月 28 日

0 投票

%%
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
  • 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.
Test this script with other input columns.

6 件のコメント

soe thiha
soe thiha 2020 年 9 月 28 日
編集済み: soe thiha 2020 年 9 月 28 日
Thanks for your kind advice.
I want to get is the total number of continuous/consecutive groups of 1 values. There are only 2 groups of continuous 1 values (index 3 to 4 and index 6 to 8). I don't want to get all 1 values groups.
per isakson
per isakson 2020 年 9 月 28 日
I don't understand your comment.
"There are only 2 groups of continuous 1 values (index 3 to 4 and index 6 to 8)." Isn't that exactly what my script returns?
soe thiha
soe thiha 2020 年 9 月 28 日
編集済み: soe thiha 2020 年 9 月 28 日
Hello Sir
Yes your codes is exactly perfet. And, I am sorry for my misunderstanding.
But, if I want to get the total number of groups, how can I do that,Sir.
Thank you for your kind helps.
Regards,
per isakson
per isakson 2020 年 9 月 28 日
"total number of groups" that's given by the number of rows of the output matrix. Thus, use
number_of_groups = size( output_of_script, 1 );
soe thiha
soe thiha 2020 年 9 月 28 日
編集済み: per isakson 2020 年 9 月 28 日
Dear Per Isakson
Thank you very much for your helps.
If you don't mind, may I request a next small question please?
Now I got the number of continuous 1 values groups. But, if I want to get the maximum value corresponding to the continuous groups, how can I do that please? I cannot use the value that is corresponding to 0 or individual 1 values. I have to find the max value within the continuous 1 groups .Follow is the sample data in which the answer is 2234.18.
value index
849.85 0
799.95 1
946.62 1
801.16 1
857.09 1
829.44 0
1569.19 0
2234.79 1
2168.86 0
2234.18 1
1556.63 1
962.04 0
838.04 1
774.14 1
827.39 1
754.04 1
857.09 1
915.24 1
per isakson
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 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2020 年 9 月 27 日

コメント済み:

2020 年 9 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by