how can i resolve the problem of using indices?

1 view (last 30 days)
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.
  1 Comment
per isakson
per isakson on 6 Oct 2015
(j/2) cannot be used as an index, since it is not guaranteed to be an integer

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Oct 2015
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)
  6 Comments
Walter Roberson
Walter Roberson on 7 Oct 2015
You need to define the relationship between a duration of "T" and an indices. If the duration is in terms of indices then:
T = 15; %duration of pulse in bits
Ndata = 8;
output = zeros(T, Ndata);
inputvals = randint(1, Ndata);
for j = 1 : length(inputvals)
if inputvals(j) == 1
output(1:floor(T/2), j) = 1;
% output(floor(j/2)+1:end, j) = 0; %already 0 do not need to set it
else
% output(:,j) = 0; %already 0 do not need to set it
end
end
output = reshape(output, 1, []);
This can be further optimized:
T = 15; %duration of pulse in bits
Ndata = 8;
output = zeros(T, Ndata);
inputvals = randi([0,1], 1, Ndata);
output(1:floor(T/2), logical(inputvals)) = 1;
output = reshape(output, 1, []);
s
s on 7 Oct 2015
inputvals =
1 0 1 0 0 0 1 0
output =
0 0 0 0 0 0 0 0
I got this ouput for T=1 and Ndata=8 using the above code.This is not the desired output.I have attached the document for your reference.I am talking about unipolar_Rz.

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!