Problem with my code- trying to iterate through a matrix

4 views (last 30 days)
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 Comments
Max Rayne
Max Rayne on 4 Jan 2019
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

Sign in to comment.

Answers (2)

Stephan
Stephan on 4 Jan 2019
Edited: Stephan on 4 Jan 2019
i think this is correct:
[x0]=rand_values(index,1);
[y0]=rand_values(index,2);

Bob Thompson
Bob Thompson on 4 Jan 2019
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!