MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

OzzWal
0

"Index exceeds matrix dimensions" while loop condition

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

  0 件のコメント

サインイン to comment.

製品


リリース

R2017b

2 件の回答

回答者: 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 件のコメント

OzzWal
2019 年 1 月 20 日
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
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
2019 年 1 月 20 日
Okay, I will do in future,thank you - I now am able to concatenate all the coordinates.

サインイン to comment.


回答者: 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
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
2019 年 1 月 20 日
Did you overlook my answer below?

サインイン to comment.



Translated by