??? Subscript indices must either be real positive integers or logicals
Mostrar comentarios más antiguos
I have a for loop that gives the values of x(n) -- here n is an index.
d=0.5;
x(1)=1;
for n=2:10
x(n)= x(n-1)+d;
y(n)= log(abs(x(n)));
fprintf('y(%d)=%d', n, y(n))
end
I want the loop to also give the values of y(n) = log(abs(x(n))).
However, MATLAB doesn't seem to like this definition and I am presented with the error message: ??? Subscript indices must either be real positive integers or logicals.
What should I do to fix it?
Thanks.
Respuesta aceptada
Más respuestas (2)
Jan
el 27 de Dic. de 2011
Either n is not an integer greater than 0 or the symbols log or abs have been defined as variables, such that the built-in functions are shadowed. You can test this by using:
dbstop if error
and start the program. Then Matlab stops when the error occurs and you can check the symbols by:
which abs
which log
The command whos can be helpful also.
6 comentarios
Brooke
el 27 de Dic. de 2011
Sean de Wolski
el 27 de Dic. de 2011
when you issue
dbstop if error
and then run the function/script/code producing the error, what does n equal when it stops?
Brooke
el 27 de Dic. de 2011
Walter Roberson
el 27 de Dic. de 2011
Does that mean that if you comment out the assignment to y(n) that your code will start? If that is the case then you would have gotten a very different error than the one you describe: it takes serious syntax errors to keep the code from running at all.
Please make things faster for yourself by posting the relevant code so that we are not left guessing about what is going on.
Brooke
el 27 de Dic. de 2011
texas A&M universdity
el 13 de En. de 2012
Thanks, That works
Sean de Wolski
el 27 de Dic. de 2011
x(n)= x(n-1)+d;
when n = 1, the first iteration of the loop, you're trying to reference x(1-1) a.k.a. x(0) which is undefined and the reason you're seeing the above error.
1 comentario
Brooke
el 27 de Dic. de 2011
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!