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

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

Have you got a variable called unifrnd shadowing the unifrnd function?
What does the output of
which unifrnd
return?
It will returns a value in the range -1 and 1
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.
The output of of which unifrnd is C:\Program Files\MATLAB\R2010a\toolbox\stats\unifrnd.m
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?
the same error is displayed when the code is like this for i=1:3 for j=1:5 x(i,j) = y(2,j)-y(3,j); end end
Show the matrix you're using for y to make it easy for us to run your code and fix it.
y = [12 65 34 98 45; 64 23 67 12 89; 56 98 83 02 10];
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).
@balaji: Guillaume is right: This cannot be the problem. Please post some code, which reproduces the error.
The code is here with all details y=[16 5 29 28 9 15 24 30 38 8 7 5 35 17 5 33 6 45 5 18 22 25 12 41 44 46 44 49 38 30 33 7 11 5 17 39 23 42 37 25 41 30 43 46 22 2 39 48 39 45 35 32 46 6 8 4 45 25 44 47 37 15 43 16 46 48 28 21 24 4 46 49 37 39 28 8 22 23 50 39 38 47 34 21 7 28 35 38 31 9 14 37 38 44 7 19 48 7 38 31 19 6 3 17 40 18 24 14 6 25 11 35 38 17 16 34 40 40 42 10 37 19 48 30 22 41 47 8 22 29 9 12 22 28 31 35 44 49 43 39 27 8 30 40 44 15 27 36 22 29 11 5 11 42 13 4 35 8 28 34 24 17 39 7 17 27 41 4 10 27 38 41 12 28 28 35 39 26 40 49 19 25 44 5 27 6 45 16 15 5 20 25 31 13 42 31 17 20 43 6 40 12 31 37 27 40 48 10 20 18 45 4 27 33 16 23 26 11 21 41]; for i=1:10 r1=round(unifrnd(1,10)); while (r1==i) r1=round(unifrnd(1,10)); end for j=1:22 x(1,j)=0; x(1,j)=round((y(i,j)+y(r1,j))/2); end x; xx=round(unifrnd(1,2)); x(1,:)=xx*x(1,:); x; for j=1:22 diff(1,j)=0; diff(1,j)=rand*(y(5,j)-x(1,j)); end end
Jan
Jan le 13 Oct 2014
Modifié(e) : Jan 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".
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.

Réponses (1)

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.

Tags

Question posée :

le 12 Oct 2014

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by