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
Problem Comments
-
1 Comment
Dyuman Joshi
on 4 Jul 2023
Good problem, @Marcos.
Though it is a good practice to include more test cases (possibly with different combinations/permutations of inputs) in a problem, to improve the quality of the problem.
Solution Comments
Show commentsProblem Recent Solvers7
Suggested Problems
-
Project Euler: Problem 2, Sum of even Fibonacci
2299 Solvers
-
Return the first and last characters of a character array
9918 Solvers
-
964 Solvers
-
915 Solvers
-
28 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!