フィルターのクリア

Find groups of 1's in array

22 ビュー (過去 30 日間)
ali
ali 2015 年 10 月 2 日
コメント済み: Image Analyst 2015 年 10 月 15 日
Sorry but ı have asked question wrong.
thanks all for great attention.
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 1's group as a 2 there.
Examples: 1111111111100111111111111111011111111111111111= Result:1
1111111111100000000000000000111111111111111111= Result:2
11111111111000000000001111111111110000000001111111111100000000111111111 =Result :4
111111011111111010111111111101111111010011111111101001111111= Result :1
111010101111101000000011100011111 =RESULT :3 LIKE THAT
  3 件のコメント
ali
ali 2015 年 10 月 15 日
thanks for attention. For example number of zeros is less than 4 then neglect it and it could be one group with the others
Image Analyst
Image Analyst 2015 年 10 月 15 日
So ignore 3 or less. I've given you an answer that works. But you have not answered Thorsten or my questions on why you say it doesn't.

サインインしてコメントする。

採用された回答

Thorsten
Thorsten 2015 年 10 月 2 日
Nsplit = 5; % number of consecutive zeros that split a group
Z = find(diff(x) == 1) - find(diff(x) == -1);
Ngroups = sum(Z > Nsplit) + 1;
  7 件のコメント
Thorsten
Thorsten 2015 年 10 月 8 日
This file has 2337 groups of zeros. The largest group are 25 zeros, which occurs 11 times. You need a different algorithm to come up with 4 groups of 1's. How do you know that the answer is 4?
Image Analyst
Image Analyst 2015 年 10 月 8 日
I also asked the very same thing, and got no good answer.

サインインしてコメントする。

その他の回答 (3 件)

Image Analyst
Image Analyst 2015 年 10 月 6 日
Ali, if you have the Image Processing Toolbox, this will give you exactly the results you want:
b = [1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] % Result:1
filtered_b = ~bwareaopen(~b, 3) % Remove short stretches of 0's.
[~, numRegions] = bwlabel(filtered_b) % Count # regions of 1's
b = [1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] % Result:2
filtered_b = ~bwareaopen(~b, 3) % Remove short stretches of 0's.
[~, numRegions] = bwlabel(filtered_b) % Count # regions of 1's
b = [1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1] % Result :4
filtered_b = ~bwareaopen(~b, 3) % Remove short stretches of 0's.
[~, numRegions] = bwlabel(filtered_b) % Count # regions of 1's
b = [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1] % Result :1
filtered_b = ~bwareaopen(~b, 3) % Remove short stretches of 0's.
[~, numRegions] = bwlabel(filtered_b) % Count # regions of 1's
b = [1,1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1] % RESULT :3 LIKE THAT
filtered_b = ~bwareaopen(~b, 3) % Remove short stretches of 0's.
[~, numRegions] = bwlabel(filtered_b) % Count # regions of 1's
If you like my answer, please click on the "0 votes" under my avatar to vote for it.
  9 件のコメント
Image Analyst
Image Analyst 2015 年 10 月 7 日
Can you tell me the indexes of where your 4 regions start and stop?
ali
ali 2015 年 10 月 7 日
Sorry but exactly ı dont know the real indexes because ı tried it on simulink for compare the results. Simulink gives the 4 value.

サインインしてコメントする。


ali
ali 2015 年 10 月 6 日
It doesnt work for a group number is more than one why. Everytime it find group number 1
  2 件のコメント
Stephen23
Stephen23 2015 年 10 月 6 日
Please do not post comments in an Answer. You can write comments to the question, or any of the answers.
ali
ali 2015 年 10 月 6 日
just ı did that wrongly the answer not accepted how can ı do that

サインインしてコメントする。


Image Analyst
Image Analyst 2015 年 10 月 6 日
Looks like you've accepted an answer that works for you. Al alternate way though is to use the Image Processing Toolbox if you have it. Then you can use bwareaopen() to get rid of small groups of zeros, then use bwlabel() to count the groups of 1's.
  1 件のコメント
ali
ali 2015 年 10 月 6 日
ı did it wrongly it doenst accepted.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by