MATLAB: How can i resolve the problem of using indices

i am writing a code for unipolar RZ.Where '1' in the input is transformed to 1 and 0 over the same period of input.whereas 0 in the input remains as 0 throughout the period. The code goes like this
Ndata=8
input = randint(1, 2*Ndata); % make polar signal
for j=1:length(input)
if input(j)==1
output(j:(j/2))=1;
output((j/2):((j/2)+1))=0;
else
output(j:(j/2))=0;
output((j/2):((j/2)+1))=0;
end
end
it gives me an error showing that "Warning: Integer operands are required for colon operator when used as index "??? "Subscript indices must either be real positive integers or logicals".
Kindly resolve my problem. Thanks.

Best Answer

As per indicates, j/2 might not be integral. In your expression
j:(j/2)
that does not matter because the rules of the colon operator increment by 1 starting from the initial value and the initial j will be integral, so the non-integral j/2 will never be generated.
On the other hand, j/2 will always be less than j unless j is negative, and you cannot have negative indices. So j:j/2 will always be empty (or invalid anyhow.) And if you switch to j/2:j then you do hit the problem of non-integral indices.
You need to be concerned with using floor() to force integral values.
Caution: randint is going away. You should replace it with
randi([0 1], 1,2*Ndata)