Comparing elements in a 2d array

1 次查看(过去 30 天)
Nicholas Mosca
Nicholas Mosca 2020-7-22
编辑: Matt J 2020-7-22
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);

回答(1 个)

Matt J
Matt J 2020-7-22
编辑:Matt J 2020-7-22
>> 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!

Translated by