Find groups of 1's in array
Show older comments
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 Comments
Eng. Fredius Magige
on 2 Oct 2015
Edited: Eng. Fredius Magige
on 2 Oct 2015
Hi Complex question, since noted for few 0 you neglet (but how much is few, in which sequence by 1 or 2 or 3 etc) this info needed to handle your requirement as well as to be in array/matrix formate Be specific, please
ali
on 15 Oct 2015
Image Analyst
on 15 Oct 2015
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.
Accepted Answer
More Answers (3)
Image Analyst
on 6 Oct 2015
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 Comments
Image Analyst
on 6 Oct 2015
The second argument is the length of the stretches of zeros that you want to ignore or keep. You said that you want to consider any stretch of zeros that is 25 long as a stretch of 1's.
ali
on 6 Oct 2015
Image Analyst
on 6 Oct 2015
The variable deneme in your mat file has 2338 stretches of 1's and 2339 stretches of zeros. Here is code I wrote to determine the lengths of the stretches you have:
load('matlab.mat');
% Look at the stretches of 1's
[labeledData, numRegions] = bwlabel(deneme); % Count # regions of 1's
measurements = regionprops(labeledData, 'Area');
all1Areas = [measurements.Area];
% Find unique areas
uniqueAreas1 = unique(all1Areas)
[labeledDatanumRegions] = bwlabel(~deneme); % Count # regions of 0's
measurements = regionprops(labeledData, 'Area');
all0Areas = [measurements.Area];
% Find unique areas
uniqueAreas0 = unique(all0Areas)
And here are the results:
uniqueAreas1 =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 146
uniqueAreas0 =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 146
What lengths of 0's do you want to replace with 1's?
ali
on 6 Oct 2015
Image Analyst
on 6 Oct 2015
I know that but you need to specify the number of zeros in a row that you'll ignore. For these cases you're saying if there are 3 or more zeros, don't join the neighboring 1's. But if there are 1 or 2 0's to join the neighboring 1's. If you use that rule, then for your super long "deneme" variable, you're going to have more than 4 separate stretches of 1's. Why do you think you should have just 4???
ali
on 7 Oct 2015
Image Analyst
on 7 Oct 2015
Can you tell me the indexes of where your 4 regions start and stop?
ali
on 7 Oct 2015
Image Analyst
on 6 Oct 2015
0 votes
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.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!