Count the "areas" of zeros and the "areas" of ones in a vector?
1 次查看(过去 30 天)
显示 更早的评论
I have a vector:
a=[0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1 ];
in which I would like to count the "areas" of zeros and the "areas" of ones. In the example above I have
4 areas of zeros and 4 areas of ones. Thank you!
0 个评论
采纳的回答
Andrei Bobrov
2013-6-21
a=[0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1 ];
lc = [true;diff(a(:))~=0];
x = a(lc);
zerosareas = sum(~x);
onesareas = sum(x);
更多回答(2 个)
Jan
2013-6-21
a = [0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1 ];
b = RunLength(a);
nZero = sum(b == 0);
nOne = length(b) - nZero; % Or: sum(b == 1)
2 个评论
Image Analyst
2013-6-23
I know you already accepted an answer, but if you have the Image Processing Toolbox there is a straightforward one-liner solution to get either of those numbers:
a=[0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 0 1];
[~, numberOf1Regions] = bwlabel(a) % Get # of "1" regions.
[~, numberOf0Regions] = bwlabel(~a) % Get # of "0" regions.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!