Problem with my code- trying to iterate through a matrix
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!