Problem with my code- trying to iterate through a matrix

3 ビュー (過去 30 日間)
Max Rayne
Max Rayne 2019 年 1 月 4 日
編集済み: Stephan 2019 年 1 月 4 日
Hi,
So i've got the code as below. I've been given x & y coordinate limits. I've generated a random number matrix of 150 values and scaled this up to match the x y coords limits. I've written a function previously to work out the gradient descent. With this code, I'm trying to set up a for loop which goes through this matrix and takes each pair of x y coordinates, takes them to be x0 and y0 (which is the input for the function I've written) and then collect and collect all end points.
however this doesnt work with my code below, would anyone be able to identify why ?
xcoords = [-9:0.2:9];
ycoords = [-8:0.2:8];
[x,y]=meshgrid(xcoords,ycoords);
gamma=0.2;
threshold=0.0002;
[z] = comp_z(x,y);
x_rand=-9+(18).*rand(150,1);
y_rand=-8+(16).*rand(150,1);
rand_values=[x_rand,y_rand];
imagesc(z)
hold on
for index= 1:length(rand_values)
[x0]=rand_values(index,:);
[y0]=rand_values(index,:);
[fvals]=gradient_descent(x,y,z,x0,y0,threshold,gamma);
line(fvals(end,1), fvals(end,2),'marker','*')
end
hold off
  2 件のコメント
Stephan
Stephan 2019 年 1 月 4 日
編集済み: madhan ravi 2019 年 1 月 4 日
we need to know comp_z
Max Rayne
Max Rayne 2019 年 1 月 4 日
sorry should have clarified, comp_z is just a function i've written to work out values for z according to a specific equation im using, so that part should work fine

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

回答 (2 件)

Stephan
Stephan 2019 年 1 月 4 日
編集済み: Stephan 2019 年 1 月 4 日
i think this is correct:
[x0]=rand_values(index,1);
[y0]=rand_values(index,2);

Bob Thompson
Bob Thompson 2019 年 1 月 4 日
Why are you setting both x0 and y0 equal to the ordered pair of your 'origin'?
[x0]=rand_values(index,:);
[y0]=rand_values(index,:);
I would think that you want to have each be a single value such that they make an ordered pair together.
[x0]=rand_values(index,1);
[y0]=rand_values(index,2);
Let me know if this is not what you were intending. I do not have a working knowledge of gradient_descent() or imagesc() so I might be misinterpreting what you are trying to accomplish.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by