Counting changes binary number batch

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Author Block Masks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by