Counting changes binary number batch

1 view (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jan 2013
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 Comments
Lily
Lily on 16 Jan 2013
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 on 16 Jan 2013
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) );

Sign in to comment.

More Answers (0)

Categories

Find more on Author Block Masks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!