While running program it shows an error Subscript indices must either be real positive integers or logicals.
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 12 Oct 2014
Have you got a variable called unifrnd shadowing the unifrnd function?
What does the output of
which unifrnd
return?
balaji
le 12 Oct 2014
Image Analyst
le 12 Oct 2014
That did not answer the question - not sure why you thought it did. Please copy and paste the result of what Guillaume said to do back here.
balaji
le 12 Oct 2014
Guillaume
le 12 Oct 2014
unifrnd is the only expression that has negative values explicitly passed to it, hence why I thought it may be the cause of the error. The other possibility would be round, again if it's variable shadowing the function.
Does unifrnd(-1, 1) works if you call it just before the loops?
balaji
le 12 Oct 2014
Image Analyst
le 12 Oct 2014
Show the matrix you're using for y to make it easy for us to run your code and fix it.
balaji
le 12 Oct 2014
Guillaume
le 12 Oct 2014
There must be something that you're not telling us, because there's no way that:
for i=1:3
for j = 1:5
x(i,j) = y(2,j) - y(3,j);
end
end
generates that error, regardless of the values of y.
What is the full text of the error you're getting (that is copy/paste everything in red).
Jan
le 12 Oct 2014
@balaji: Guillaume is right: This cannot be the problem. Please post some code, which reproduces the error.
balaji
le 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
le 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).
Finally, you seem to be using a variable named diff. Rename it to something else as it is shadowing matlab's diff function.
Réponses (1)
Jan
le 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.
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!