"Index exceeds matrix dimensions" while loop condition

1 回表示 (過去 30 日間)
OzzWal
OzzWal 2019 年 1 月 19 日
コメント済み: OzzWal 2019 年 1 月 20 日
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

採用された回答

Image Analyst
Image Analyst 2019 年 1 月 19 日
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 件のコメント
Image Analyst
Image Analyst 2019 年 1 月 20 日
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 2019 年 1 月 20 日
Okay, I will do in future,thank you - I now am able to concatenate all the coordinates.

サインインしてコメントする。

その他の回答 (1 件)

Nathaniel Tarantino
Nathaniel Tarantino 2019 年 1 月 19 日
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 件のコメント
OzzWal
OzzWal 2019 年 1 月 20 日
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?
Image Analyst
Image Analyst 2019 年 1 月 20 日
Did you overlook my answer below?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by