Comparing elements in a 2d array

1 view (last 30 days)
Nicholas Mosca
Nicholas Mosca on 22 Jul 2020
Edited: Matt J on 22 Jul 2020
I would like to know if it is possible to find the peak numbers, numbers that are larger then the value befroe and value after. Thank you in advance
array = [1 2 1 3 4 6 4];
peak = 0;
for i = 1:length(array)
if array(i) > array(i)-1||array(i)<array(i)+1
peak = peak + 1;
end
end
disp(peak);

Answers (1)

Matt J
Matt J on 22 Jul 2020
Edited: Matt J on 22 Jul 2020
>> peak = [0, diff(array(1:end-1))>=0 & diff(array(2:end))<=0 , 0]
peak =
0 1 0 0 0 1 0
>> array
array =
1 2 1 3 4 6 4

Community Treasure Hunt

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

Start Hunting!