フィルターのクリア

Counting changes binary number batch

2 ビュー (過去 30 日間)
Lily
Lily 2013 年 1 月 16 日
Hi If I have a binary data f.ex. (see data below) would it be possible to count how often it changes and say that we don't count it as a change if one number in a long run of number variates?
data = [ 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 1];
For this data we would say we don't count take data(1) but we count data(2:6) as a change. Then data(7:12) and that data(9) doesn't effect the count.
Is it possible to construct a general code for this?

採用された回答

Walter Roberson
Walter Roberson 2013 年 1 月 16 日
t = data;
t(2:end-1) = t(2:end-1) | (t(1:end-2) & t(3:end));
numchange = sum( t(1:end-1) ~= t(2:end) );
  2 件のコメント
Lily
Lily 2013 年 1 月 16 日
Yes this is a good solution if you just want to count the change but not if you take in to account if one number is another then the row f.ex 0 0 1 0 0 it count see it as 0 0 0 0 0. And also iff the data would start with another number or start as a row :)
Could you help me with that?
Walter Roberson
Walter Roberson 2013 年 1 月 16 日
The second line was intended to adjust for those cases, but I realize now it only adjusts for the case of a 0 in the middle of 1's.
Let's see...
t = data;
mask = (t(1:end-2) ~= t(2:end-1)) & (t(1:end-2) == t(3:end));
t([false mask false]) = ~t([false mask false]);
numchange = sum( t(1:end-1) ~= t(2:end) );

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAuthor Block Masks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by