MATLAB: Index in postition 1 is invalid. Array indices must be positive integers or logical values.

3d plotsfor loopmatrix array

Trying to write a loop to fill in the values of a 3d array of target coordinates and then one matrix that has the third variable as theta, and the other as velocity, but keep getting this error. eventually wanting to plot these.

Best Answer

Your Hmin is negative. Your y starts at Hmin so your y starts negative. You use y as a subscript, so that means that you are using a negative value as a subscript.
You need to decide whether you are creating a formula, or an array.
A formula can potentially accept any inputs, including fractional or infinite, but it does not define an ordering: for formula F, f(x, y) is in no way "beside" f(x, y+1) since you might also have f(x, y+0.01) and so on.
An array on the other hand restricts subscripts to be positive integers. With an array, you can talk about the "first" and "second" entry and so on (ordering) but it does not in itself describe how the "first" or "second" correspond to anything in the real or mathematical world. You might have other information to know that, for example, A(5,1) corresponds to x = 17 and y = -2, and that A(5,2) corresponds to y = -1.99, but that is not "baked in" to the array: MATLAB just knows "first", "second" and so on.
What do you do? Well, in the special case that your values are mathematically spaced 1 unit apart, then even if they do not start at an integer, you can use A(x-firstX+1, y-firstY+1) to map from mathematical values x, y to index values. I would, however, recommend round() of the expressions if firstX or firstY are not integers.