Problem with my code- trying to iterate through a matrix

조회 수: 4 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by