Borrar filtros
Borrar filtros

Counting changes binary number batch

2 visualizaciones (últimos 30 días)
Lily
Lily el 16 de En. de 2013
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de En. de 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 comentarios
Lily
Lily el 16 de En. de 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 el 16 de En. de 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) );

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Author Block Masks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by