Problem 58473. Finding valleys

You have a vector of altitudes (units are arbitrary) and need to find the depths of all the valleys. You also need to determine how wide the base of each valley is, the index of the left side of each valley, and the index of the right side of each valley.
For example, given a vector like this:
D = [2 3 4 3 2 1 1 1 2 3 4 5 4 3 4 5]
There are two valleys. The first has a depth of 1 and its base is three units long. The index of the base's left side is 6, the index of the base's right side is 8. The second valley has a depth of 3 and its base is one unit long. The index of its left and right sides is 14. You should return a matrix of four rows. The first row should list the depths, the second row should list the widths, the third row should list the left sides, and the final row should list the right sides.
For the above example, you would return a matrix of size 4,2 with the following values:
V = [ 1 3 ; 3 1 ; 6 14 ; 8 14 ]
Notice that the first index is not part of a valley since you don't know if the value that preceded it was higher or lower. More generally, the first and last indexes of the input vector will never be considered valleys, even if they are smaller than their one neighbor.

Solution Stats

100.0% Correct | 0.0% Incorrect
Last Solution submitted on Nov 29, 2023

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers7

Suggested Problems

Problem Tags

Community Treasure Hunt

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

Start Hunting!