Index in position 1 is invalid. Array indices must be positive integers or logical values.

2 次查看(过去 30 天)
I've attached my code below. I keep getting the issue of incorrect indexing but I'm not sure why this is as I have defined my for loop starting at 1. If someone could take a quick look and help me that would be great. I feel like I'm just overlooking something. The error occurs in line 36
  4 个评论
Rik
Rik 2018-10-6
The same goes for using i as a loop counter. If by some strange reason it gets interpreted as the imaginary unit, you'll get these kinds of errors.

请先登录,再进行评论。

采纳的回答

jonas
jonas 2018-10-6
编辑:jonas 2018-10-6
Here is something fishy.
sigma = func2(t,x,sigma_x,func1(kc,t))
...
function sigma = func2(t,M,sigma_M,func1)
If you want to pass the output of func2 as an input to func1 that's ok, but do not name the output func1. This confusion causes func1 to be interpreted as a variable, which is why you get this error. You can pass the function as an argument like you do, but you need to name the variable used in func2 something other than func1.
What you do is similar to writing:
func1 = func1(arg1,arg2)
func1 is now a variable, so the next time you call it it will be interpreted as such.
  2 个评论
jonas
jonas 2018-10-6
编辑:jonas 2018-10-6
Well no wonder, since you changed both the variable name and the function call to func11... don't use obfuscating variable names if you want to write clear easy-to-debug code. If you still do not see it..
total = total + ((func11(temp1,t(n)) - func11(temp2,t(n)))/2)^2;
remove these 1's, and you will be able to run the code. You are trying to use the function here right, and not a variable?
I don't even know why you pass the func11 variable to the func2 function, it seems you are not even using it.
PenguinForce
PenguinForce 2018-10-7
Ah! Thank you so much! You are absolutely right. I'm not really using them. Thank you again for your time!! :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by