From: Dave Smith on
Anyone know what wrong with this code? This is what I'm trying to do:

0 = no transition at the beginning of interval (one bit time)
1 = transition at beginning of interval

B=[0 1 0 0 1 1 0 0 0 1 1];
[row,column]=size(B);

for c=1:column;
B(1)=-1;
if B(c)==0;
B(c)=B(1);
end
if B(c)==1 && B(c-1)==1;
B(c)=-1;
end
if B(c)==1 && B(c-1)==-1;
B(c)=1;
end
end

Basically, tt starts about -1. If it see a 0, it just stay at the level of the previous bit. If it see a 1, it transition to a 1 or -1 depending on the previous level. So level 1 go to level -1 when a bit 1 happened, vice versa.
From: Dave Smith on
Here is the updated code:

B=[0 1 0 0 1 1 0 0 0 1 1];
[row,column]=size(B);

if B(1)==0;
B(1)=-1;
end
if B(1)==1;
B(1)=1;
end
for c=2:column;
if B(c)==0;
B(c)=B(c-1);
end
if B(c)==1 && B(c-1)==1;
B(c)=-1;
end
if B(c)==1 && B(c-1)==-1;
B(c)=1;
end
end

It seems to messed up going from column 3 to column 4. Since column 3 = 0, it suppose to stay the same as column 2 which 1, but for some reason it transited to -1. All the other column seems fine. Does anyone have a solution to this mistake? I'm not sure if I'm using && correctly.
From: Walter Roberson on
Dave Smith wrote:
> Here is the updated code:
>
> B=[0 1 0 0 1 1 0 0 0 1 1];
> [row,column]=size(B);
>
> if B(1)==0;
> B(1)=-1;
> end
> if B(1)==1;
> B(1)=1;
> end
> for c=2:column;
> if B(c)==0;
> B(c)=B(c-1);
> end
> if B(c)==1 && B(c-1)==1;
> B(c)=-1;
> end
> if B(c)==1 && B(c-1)==-1;
> B(c)=1;
> end
> end

You are asking for trouble by modifying B in-place. Construct a new vector the
same size as B but which has the desired outputs, leaving B as-is.

Based upon your earlier description that a 1 indicates a transition:

current_state = -1;
D = zeros(size(B));
for c = 1:length(B))
if B
current_state = - current_state;
end
D(c) = current_state;
end
From: Walter Roberson on
Walter Roberson wrote:

> Based upon your earlier description that a 1 indicates a transition:
>
> current_state = -1;
> D = zeros(size(B));
> for c = 1:length(B))
> if B

Sorry, make that B(c)

> current_state = - current_state;
> end
> D(c) = current_state;
> end