Cody

# Problem 1366. Finding peaks

Solution 2049922

Submitted on 7 Dec 2019 by Asif Newaz
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
x = [2 12 4 6 9 4 3 1 19 7]; y_correct = [12 9 19]; assert(isequal(find_peak(x),y_correct))

xDiff = 10 -8 2 3 -5 -1 -2 18 -12 a = 1 -1 1 1 -1 -1 -1 1 -1 xZeroCross = -2 2 0 -2 0 0 2 -2 indexDown = 2 5 9 y = 12 9 19

2   Pass
x = [ones(1,9) 10 zeros(1,10)]; y_correct = 10; assert(isequal(find_peak(x),y_correct))

xDiff = 0 0 0 0 0 0 0 0 9 -10 0 0 0 0 0 0 0 0 0 a = 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 xZeroCross = 0 0 0 0 0 0 0 1 -2 1 0 0 0 0 0 0 0 0 indexDown = 10 y = 10

3   Pass
x = [1:10 100 -4 2 0 0 4 5 6 0]; y_correct = [100 2 6]; assert(isequal(find_peak(x),y_correct))

xDiff = 1 1 1 1 1 1 1 1 1 90 -104 6 -2 0 4 1 1 -6 a = 1 1 1 1 1 1 1 1 1 1 -1 1 -1 0 1 1 1 -1 xZeroCross = 0 0 0 0 0 0 0 0 0 -2 2 -2 1 1 0 0 -2 indexDown = 11 13 18 y = 100 2 6

4   Pass
x = [1:5 6:-1:1 1:3 4:-1:0]; y_correct = [6 4]; assert(isequal(find_peak(x),y_correct))

xDiff = 1 1 1 1 1 -1 -1 -1 -1 -1 0 1 1 1 -1 -1 -1 -1 a = 1 1 1 1 1 -1 -1 -1 -1 -1 0 1 1 1 -1 -1 -1 -1 xZeroCross = 0 0 0 0 -2 0 0 0 0 1 1 0 0 -2 0 0 0 indexDown = 6 15 y = 6 4

5   Pass
x = zeros(1,10000); x(2:10:9992) = 1; y_correct = ones(1,1000); assert(isequal(find_peak(x),y_correct))

xDiff = Columns 1 through 30 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 31 through 60 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 61 through 90 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 91 through 120 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 121 through 150 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 151 through 180 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 181 through 210 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 211 through 240 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 241 through 270 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 271 through 300 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 301 through 330 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 331 through 360 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 361 through 390 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 391 through 420 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 421 through 450 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 451 through 480 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 481 through 510 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 511 through 540 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 541 through 570 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 571 through 600 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 601 through 630 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 631 through 660 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 661 through 690 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 691 through 720 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 721 through 750 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 751 through 780 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 781 through 810 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 811 through 840 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 841 through 870 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 871 through 900 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 901 through 930 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 931 through 960 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 961 through 990 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 991 through 1020 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1021 through 1050 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1051 through 1080 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1081 through 1110 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1111 through 1140 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1141 through 1170 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1171 through 1200 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1201 through 1230 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1231 through 1260 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1261 through 1290 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1291 through 1320 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1321 through 1350 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1351 through 1380 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1381 through 1410 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 Columns 1411 through 1440 1 -1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0...

6   Pass
x = [1 2 4 7 10 9 8 9 10 9 7 4 5 3 1 -2 -3 0 1 0]; y_correct = [10 10 5 1]; assert(isequal(find_peak(x),y_correct))

xDiff = 1 2 3 3 -1 -1 1 1 -1 -2 -3 1 -2 -2 -3 -1 3 1 -1 a = 1 1 1 1 -1 -1 1 1 -1 -1 -1 1 -1 -1 -1 -1 1 1 -1 xZeroCross = 0 0 0 -2 0 2 0 -2 0 0 2 -2 0 0 0 2 0 -2 indexDown = 5 9 13 19 y = 10 10 5 1

7   Pass
x = [repmat([1 2 3 2 1],[1,1000])]; y_correct = 3*ones(1,1000); assert(isequal(find_peak(x),y_correct))

xDiff = Columns 1 through 30 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 31 through 60 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 61 through 90 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 91 through 120 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 121 through 150 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 151 through 180 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 181 through 210 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 211 through 240 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 241 through 270 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 271 through 300 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 301 through 330 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 331 through 360 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 361 through 390 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 391 through 420 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 421 through 450 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 451 through 480 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 481 through 510 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 511 through 540 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 541 through 570 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 571 through 600 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 601 through 630 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 631 through 660 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 661 through 690 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 691 through 720 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 721 through 750 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 751 through 780 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 781 through 810 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 811 through 840 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 841 through 870 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 871 through 900 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 901 through 930 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 931 through 960 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 961 through 990 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 991 through 1020 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1021 through 1050 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1051 through 1080 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1081 through 1110 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1111 through 1140 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1141 through 1170 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1171 through 1200 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1201 through 1230 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1231 through 1260 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1261 through 1290 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1291 through 1320 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1321 through 1350 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1351 through 1380 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1381 through 1410 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 Columns 1411 through 1440 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0 1 1 -1 -1 0...

### Community Treasure Hunt

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

Start Hunting!