Info

This question is closed. Reopen it to edit or answer.

While running program it shows an error Subscript indices must either be real positive integers or logicals.

1 view (last 30 days)
The Code is here
for i=1:3
for j=1:5
x(i,j) = round(y(i,j)+(unifrnd(-1,1)*(y(2,j)-y(3,j))));
end
end
Please help me to overcome the above error
  13 Comments
Jan
Jan on 13 Oct 2014
Edited: Jan on 13 Oct 2014
Please, balaji, format your code such that it is readable. Look at the "{} Code" button.
Lines like "x;" are meaningless and increase the level of confusion only.
"diff" is the name of the function. Do not cover it by using it as a name of a variable.
Please add a complete copy of the error message, most of all the line, which causes the error. And take the chance to let the debugger locate the problem for you, as explained in my answer. This is much smarter than asking the members of the forum to use the debugger to do the same for you.
The posted code runs without any error on my Matlab. Please copy it into a function (not a script!) to check, if you have redefined a symbol, e.g. "round".
Guillaume
Guillaume on 13 Oct 2014
Balaji, please edit your comment and use the {} code button to format your code.
As it is now, your y is a 1x220 vector, yet you try to access y(i,j) with i > 1. That is of course giving me an error (but not the one you mentioned).
Additionally, if all you want to generate are integers, you can use randi.
Finally, you seem to be using a variable named diff. Rename it to something else as it is shadowing matlab's diff function.

Answers (1)

Jan
Jan on 12 Oct 2014
The debugger is the perfect tool to investigte such problems. Type this in the command line:
dbstop if error
Then run your code again until Matlab stops, when the error occurres. Then evaluate the current line in pieces until you find out, which expression causes the error.
Using the debugger locally is much more efficient than asking the forum.

Tags

Community Treasure Hunt

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

Start Hunting!