Find easy patterns in a logic vector

2 ビュー (過去 30 日間)
fjnb86
fjnb86 2012 年 3 月 27 日
Hello everyone,
I am a little bit confused performing an easy procedure.
I have a long logic vector, for example:
R = [1 0 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 1 0 0 1]
It would be nice to localize this information in smalls groups.
For instance, in this position u have a group of three, here a group of 5, here only one.
Is there any specific command to do that?
Thanks!

採用された回答

Daniel Shub
Daniel Shub 2012 年 3 月 27 日
It doesn't handle the edges perfectly, but you might want to start with
diff(find(diff(R)))
The diff( R ) is a logic vector indicating if the ith element is end of a group. The find() gets the indices on which group end. The outer diff() then determines how many elements are between group endings.
  1 件のコメント
fjnb86
fjnb86 2012 年 5 月 3 日
Thanks Daniel
Very useful

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2012 年 9 月 5 日
I'm not sure what you mean by "localize" but maybe you mean "connected components labeling." If you have the Image Processing Toolbox, you can identify each groups of connected 1's with a label number:
R = [1 0 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 1 0 0 1]
labeled_R = bwlabel(R)
In the command window, it shows:
labeled_R =
1 0 2 2 0 0 3 3 0 0 0 0 4 4 4 4 4 0 0 5 0 0 6
If you want the indexes of each group (object, or "blob"), then you can call regionprops().
indexes = regionprops(labeled_R, 'PixelIdxList');

Community Treasure Hunt

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

Start Hunting!

Translated by