Problem 42802. Rolling maximums above a threshold

You are given either a vector or a 2-D matrix M and a threshold value of t. Write a script that will calculate how many times the maximum value increases between the first time your threshold value is exceeded and the end of the matrix. If you are given a 2-D matrix, go down the columns first and then the rows.

For example, M=magic(7), and your threshold=30

M=
[   30    39    48     1    10    19    28;  
    38    47     7     9    18    27    29;  
    46     6     8    17    26    35    37;  
     5    14    16    25    34    36    45;  
    13    15    24    33    42    44     4;  
    21    23    32    41    43     3    12;  
    22    31    40    49     2    11    20];

The first value that's higher than your threshold is 38. The 30 at the start of the matrix does not exceed your threshold, so it does not count.

Then going down the first column 46>38, so you have another new maximum.

The next value higher than the threshold is 39, but that is less than 46, so your matrix maximum is still 46 until you reach 47. The third column contains 48, and the fourth column contains 49. There are no numbers higher than 49 in the matrix, so your matrix maximum changes five times.

38-->46-->47-->48-->49.

The output to your script would be n=5. You do not need to store what the maxima are, only how many times it changes. If there are no numbers higher than the threshold in your matrix, the output should be zero.

Solution Stats

73.33% Correct | 26.67% Incorrect
Last Solution submitted on Oct 28, 2022

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers28

Suggested Problems

More from this Author80

Community Treasure Hunt

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

Start Hunting!