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

Eng. Fredius Magige
Eng. Fredius Magige 2015년 10월 2일
편집: Eng. Fredius Magige 2015년 10월 2일
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
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일

0 개 추천

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

ali
ali 2015년 10월 6일
just want to ask a question could you please explain line2 and 3 more deeply.
diff(x) is the difference of successive elements. So a row of zeros is marked by a -1 at its beginning and a 1 at its end.
find(diff(x) == 1)
Gives you the positions of all endings of rows of zeros, and
find(diff(x) == -1)
yield the positions of the endings of the sequences of zeros. So if you subtract both, Z will hold the length of the zero-sequences.
You consider only those zero-sequence above a certain length, Nsplit as valid to split a group of 1's. So
sum(Z > Nsplit)
are the valid (i.e., long enough) sequences of zeros that split the ones. The number of 1's is thus one more, +1, assumming that a sequence always starts and ends with 1's.
ali
ali 2015년 10월 6일
편집: ali 2015년 10월 6일
thank yo so much but it always finds the number of groups as 1 so it doenst work. how can we do that because ngropus always result 1 so it doesnt work for example 1111111111100000000000000000111111111111111111 the result of this array 2 and 111010101111101000000011100011111 result of this array must be 3
Make sure that you put spaces between your 0 and 1, and set Nsplit to 2 for your second example.
x = [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]
Nsplit = 2
Z = find(diff(x) == 1) - find(diff(x) == -1);
Ngroups = sum(Z > Nsplit) + 1
Ngroups =
2
x = [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]
Z = find(diff(x) == 1) - find(diff(x) == -1);
Ngroups = sum(Z > Nsplit) + 1
Ngroups =
3
ali
ali 2015년 10월 7일
how can we do that with this file it has to be 4 as a result that is the 1's group
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일

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

ali
ali 2015년 10월 6일
편집: ali 2015년 10월 6일
thanks for reply. but it finds the number of groups 12 when choose the (~b,25). But the number of groups has to be 4 how can we do that. And can we find the biggest number of zeros in array? But your code is more efficient than the others. I attached the array there.
Image Analyst
Image Analyst 2015년 10월 6일
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
ali 2015년 10월 6일
yes ı know if we give seconf argument more than 25 all time the number of groups are going to be 1. But the 25 is the last number for find the number of 1 groups as a 12 but exactly the result must be 4.
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
ali 2015년 10월 6일
the exact length may be change belong to array. for example if array 000111000111 two groups of 1 if array 011011111111 one groups of 1 if array 111111011111 one groups of 1 if array 1111111111000000000000111111111 two groups of 1. So exact number of 1 groups maybe change of zero blocks length.
Image Analyst
Image Analyst 2015년 10월 6일
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
ali 2015년 10월 7일
because ı tried it somewhere else and find this result. your comments are true.
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일

0 개 추천

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일

0 개 추천

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.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

ali
2015년 10월 2일

댓글:

2015년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by