Help me please. I am stuck with while loop to find local max/minima .

9 views (last 30 days)
I am new here and stuck with this task. I think I have done everything ringt except from the while loop.. Because I could not seem to create the path as the picture... This is the task translated in English ( sorry I am not good in English) Find indices (i, j) of the highest point, i.e. the point with the highest z-value. (Hint: Use the find). Save x, y and z values of (i, j) of each path-vector, which at this stage has only one item each. 2. Algorithm steepest descent: While point (i, j) does not lie along an edge: a. neighbor point having the lowest z-value. By neighbors meant those points that have index (i-1, j), (i + 1, j), (i, j-1), (i, j + 1). b. If this neighboring point's z-value is lower than Z (i, j): I. Do this next point about the current point (i, j). II. Add the x, y and z values of (i, j) of trail vectors so that they increase their lengths with one item each. Otherwise: Exit algorithm. c. Return to the top of the algorithm.
And this is what I have done
if true
% code
end
% Oppgave 1C
n = 45;
[X,Y,Z] = peaks(n);
mesh(X,Y,Z)
hold on
[i,j]=find(Z==max(max(Z)));
x = X(i,j);
y = Y(i,j);
z = Z(i,j);
if true
% code
end
% Oppgave 2C
while i>1&& i<n && j >1 && j<n
minNeighbour = min([Z(i-1,j),Z(i+1,j),Z(i,j-1),Z(i,j+1)]); % Oppgave 2Ca
if minNeighbour < Z(i,j) % Oppgave 2Cb
if Z(i-1,j) == minNeighbour
i = i-1;
elseif Z(1+1,j) == minNeighbour
i = i+1;
elseif Z(i,j+1) == minNeighbour
j = j+1;
else
j = j-1;
end
else
break
end
x(end +1) = X(i,j);
y(end+1) = Y(i,j);
z(end+1) = Z(i,j) ;
if true
% code
end
end
hold on
plot3(x,y,z,'-r*')
hold off
I ended up with wrong path :( and not the same as the original figure. This is my figure

Answers (1)

Kirby Fears
Kirby Fears on 30 Sep 2015
Edited: Kirby Fears on 30 Sep 2015
Your algorithm seems to work fine. It took a different path and found what looks like a lower point.
Based on the path of your solution, it looks like the very first choice of minimum neighbor was different from the intended solution. Is it possible that the starting (i,j) position (the peak) has multiple neighbors with the same minimum value? This would mean there are multiple solutions.
Try changing this part of your code to find out:
minNeighbour = min([Z(i-1,j),Z(i+1,j),Z(i,j-1),Z(i,j+1)]); % Oppgave 2Ca
Change to:
stepVec = [Z(i-1,j),Z(i+1,j),Z(i,j-1),Z(i,j+1)];
minNeighbour = min(stepVec); % Oppgave 2Ca
if sum(minNeighbor==stepVec)>1,
disp('Multiple minimums found for next iteration from ');
disp(['i = ', num2str(i), '; j = ', num2str(j)]);
end
If there are multiple matching minimum neighbors, there's nothing wrong with choosing one over the other.
If there are no matching neighbors, you will have to investigate further.
Hope this helps.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!