필터 지우기
필터 지우기

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 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