Subscript indices must either be real positive integers or logicals.

i got this error in this code i don't know why.
c=[x(1:end-1,:),x(2:end,:)];
dx=c(:,2)-c(:,1);
t=[y(1:end-1,:),y(2:end,:)];
dy=t(:,2)-t(:,1);
ra=[];
rb=[];
for i=1:size(dx)
if dx(2i-1)>0 & dy(2i)<0;
ra(2i-1)=1;
ra(2i)=1;
end
if dy(2i-1)>0 & dx(2i)<0;
rb(2i-1)=1;
rb(2i)=1;
end
end

1 Comment

Which line is the error on? You can use the stop/pause on errors debug option from the Breakpoints menu to have the code stop at the line causing the error. Then it is trivial to work out what is wrong by looking at any subscript indices you are using in the command window.

Sign in to comment.

 Accepted Answer

It's hard to tell without having some input, but these types of lines are definitely wrong:
ra(2i)=1;
i is being interpreted as imaginary, which is why you should not use i as a looping variable.
ans =
0.0000 + 2.0000i
Could be other errors as well.

6 Comments

first input is
[x,y]=ginput;
I changed i but error still appear
Yep, indeed "error still appear", but not the same error. If you change all those "2i" to, for example, "2*j", then you get the following error:
Index exceeds array bounds.
Error in matlabhelp2 (line 12)
if dx(2*j-1)>0 & dy(2*j)<0;
which is quite obvious, since dy and dx are the same size and j goes to the size of dx. You need to explain what you are trying to do.
As an aside, j also represents an imaginary number so if you don't wish to use i for that reason then you may not want to use j either. Personally I don't mind using i (so long as it is syntactically correct as a mathematical index, not the complex number) since I feel I should know in which parts of my code I expect complex numbers and which I do not expect them, but I know many people don't like to use i.
oh, that's poor advice on my part then. Lesson learned.
Personally I frequently use i as well, but the general advice on this forum tends to be to avoid using it. I will add j to the list :)
I have two matrices(dx dy), I'm looking for a condition to check 1. If the first array of dx was a positive (negative), the first array of dy be a negative (positive) array. 2. If the event is the first one, check if the odd arrays of dx and dy will all be the same and the even arrays are exactly the opposite.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!