MATLAB Answers

"Index exceeds matrix dimensions" while loop condition

15 views (last 30 days)
OzzWal
OzzWal on 19 Jan 2019
Commented: OzzWal on 20 Jan 2019
I am writing a function which implements the gradient descent algorithm.
I wish to run a while loop until the following threshold condition is met:
which adds coordinates found by the algorithm into an Nx3 matrix at each interation.
I also wish to add a starting point (coordinates which are input to the function, therefore not calculated within the loop) to this matrix.
This is my current attempt - please could you help or direct me to the relevant guides for what I want? I'm sure there are many more elegant ways to write it, but I think what I'm mostly confused about is the indexing ("index exceeds matrix dimensions") in the while loop condition.
coordsOut(j,:) = [xi, yi, zi];
xi(1) = X0;
yi(1) = Y0;
zi(1) = Z0;%add starting point
xi(2) = xi(1) - gamma*gradZi_x; %update x,y coords
yi(2) = yi(1) - gamma*gradZi_y; %%update = learning_rate * gradient_of_parameters
zi(2) = interp2(xgrid,ygrid,Z,xi(1),yi(1),'cubic');%update z with cubic interpolation from new x,y coords
gradZi_x = interp2(xgrid,ygrid,Gx,xi(1),yi(1),'cubic');%update px,py gradients
gradZi_y = interp2(xgrid,ygrid,Gy,(1),yi(1),'cubic');
if tau >sqrt((xi(2)-xi(1)^2) + (yi(2)-yi(1))^2)
g=sprintf('%d ', coordsOut);
fprintf('Answer: %s\n', g)
else
j = 2;
while tau <=sqrt((xi(j)-xi(j-1)^2) + (yi(j)-yi(j-1))^2)%threshold condition
xi(j) = xi(j-1) - gamma*gradZi_x; %update x,y coords
yi(j) = yi(j-1) - gamma*gradZi_y; %%update = learning_rate * gradient_of_parameters
zi(j) = interp2(xgrid,ygrid,Z,xi(j-1),yi(j-1),'cubic');%update z with cubic interpolation from new x,y coords
gradZi_x = interp2(xgrid,ygrid,Gx,xi(j-1),yi(j-1),'cubic');%update px,py gradients
gradZi_y = interp2(xgrid,ygrid,Gy,xi(j-1),yi(j-1),'cubic');
j = j+1;
end
g=sprintf('%d ', coordsOut);
fprintf('Answer: %s\n', g)
end

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 19 Jan 2019
Try checking the length in your while loop
while tau <=sqrt((xi(j)-xi(j-1)^2) + (yi(j)-yi(j-1))^2) && j <= length(xi) %threshold condition
If you want a warning when this happens, then add this just before the end of the while loop
j = j + 1; %( Existing code)
if j > length(xi)
warningMessage = sprintf('j (%d) is longer than length of xi (%d), so exiting loop.', j, length(xi))
uiwait(warndlg(warningMessage);
break;
end

  3 Comments

OzzWal
OzzWal on 20 Jan 2019
Thank you - I will apply more error checking once I have implemented the algorithm, but I'm supposed to write a solver which doesn't fail for any xi (so doesn't exit the loop unless threshold condition is met).
I have this at the moment, which seems to work but doesn't fill the matrix or give the entire output (cartesian coordinates at each step of the loop):
xi(1) = X0;
yi(1) = Y0;
zi(1) = Z0;%add starting point
j=1;
threshold = tau+1;
coordsOut(j,:) = [xi, yi, zi];
while tau <=threshold
j = j+1;
gradZi_x = interp2(xgrid,ygrid,Gx,xi(j-1),yi(j-1),'cubic');%update px,py gradients
gradZi_y = interp2(xgrid,ygrid,Gy,xi(j-1),yi(j-1),'cubic');
xi(j) = xi(j-1) - gamma*gradZi_x; %update x,y coords
yi(j) = yi(j-1) - gamma*gradZi_y; %%update = learning_rate * gradient_of_parameters
zi(j) = interp2(xgrid,ygrid,Z,xi(j-1),yi(j-1),'cubic');%update z with cubic interpolation from new x,y coords
threshold = sqrt((xi(j)-xi(j-1))^2 + (yi(j)-yi(j-1))^2);%threshold condition
end
g=sprintf('%d ', coordsOut);
fprintf('Answer: %s\n', g)
end
Image Analyst
Image Analyst on 20 Jan 2019
What is X0, Y0, Z0, tau, gamma, etc? Please give code that can run. If you supply the entire script, we can debug it more than if you just give us a snippet that won't even run and we have to just guess at what the problem might be, like saying to also keep track of threshold and plot it as a function of j to see how close it gets to tau. From what I see where, it looks like the x,y,z coordinates should get assigned.
OzzWal
OzzWal on 20 Jan 2019
Okay, I will do in future,thank you - I now am able to concatenate all the coordinates.

Sign in to comment.

More Answers (1)

Nathaniel Tarantino
Nathaniel Tarantino on 19 Jan 2019
I believe your problem is the placement of:
j = j+1;
By placing it at the end of the while loop, you are incrementing j without incrementing xi and yi.
When you enter the 'else' statement of your if loop:
j=2
Now you enter the while loop and evaluate the expression. Which is fine because xi and yi have two entries which have been previously defined. The code within the while loop defines xi and yi at j (which is still 2). Then at the end of the while loop you make
j = j+1;
So when the while loop comes back around to evaluate the expression. xi and yi are not defined at
j=3
and you receive the error ("index exceeds matrix dimensions") .
I didn't run the code, this was my interpretation when looking at what you had in your question. Hope this helps!
Nathan

  2 Comments

OzzWal
OzzWal on 20 Jan 2019
Thank you - that makes sense... is there a way in my current approach to get around that, or am I going to have to do some more branching/ use something different?

Sign in to comment.

Sign in to answer this question.


Translated by