I am creating a digital copolymer chain. The index is 1:30 and if the last index was A and the random number is <=0.5, the new index should be the A. If the random number is >0.5 then the new index shoud be B. The code I have isn't working and I am not sure what to do. Thank you for the help.

%chain 1

A=zeros(30,2);

B=zeros(30,2);

for i=1:30

if A(i,1)==i || rand<=0.5

A(i,2)=1;

A(i,1)=i;

else

B(i,1)=i;

B(i,2)=1;

if B(i,1)==i || rand<=0.5

A(i,1)=i;

A(i,2)=1;

else

B(i,1)=i;

B(i,2)=1;

end

end

end

%Deletes all zeros in A

TestForZero = A(:,1) == 0;

A(TestForZero, :) = []

%Deletes all zeros in B

TestForZero = B(:,1) == 0;

B(TestForZero, :) = []

%graphs

hold on

plot(A(:,1),A(:,2),'o','MarkerFaceColor',"red",'MarkerSize',15)

plot(B(:,1),B(:,2),'o','MarkerFaceColor',"blue",'MarkerSize',15)

title("Digital Copolymerchain 4")

axis off

hold off

%Deletes all zeros in A

TestForZero = A(:,1) == 0;

A(TestForZero, :) = []

%Deletes all zeros in B

TestForZero = B(:,1) == 0;

B(TestForZero, :) = []

%graphs

hold on

plot(A(:,1),A(:,2),'o','MarkerFaceColor',"red",'MarkerSize',15)

plot(B(:,1),B(:,2),'o','MarkerFaceColor',"blue",'MarkerSize',15)

title('Digital Colymerchain 1')

axis off

hold off