MATLAB: I have an array of data [ 1.1 0.5 1.8 2.5 0.6 2.0 2.1 4.0 1.3 1.4 5.0 4.2 2.2 3.2 3.4 3.5 3.3 4.8 1.11 2.0 2.5] I need a code pick data points incrementally from 1.1 until it get to the peak then start selecting after the peak in deceasing order.

I have an array of data [ 1.1 0.5 1.8 2.5 0.6 2.0 2.1 4.0 1.3 1.4 5.0 4.2 2.2 3.2 3.4 3.5 3.3 4.8 1.11 2.0 2.5] I need a code pick data points incrementally from 1.1 until it gets to the peak then start selecting after the peak in decreasing order. The expected outcome is [ 1.1 1.8 2.5 4.0 5.0 4.2 2.2 1.11] starting from the first element (1.1) the next element is 1.8 dropping 0.5 until getting to the peak (5.0) and then start decreasing from 5.0 to 1.11. the number of elements in the final output will reduce.

Best Answer

V = [1.1 0.5,1.8,2.5,0.6,2.0,2.1,4.0,1.3,1.4,5.0,4.2,2.2,3.2,3.4,3.5,3.3,4.8,1.11,2.0,2.5];
[~,idx] = max(V);
N = V(1);
X = false(size(V));
for k = 1:idx
X(k) = V(k)>=N;
N = max(V(k),N);
end
for k = idx:numel(V)
X(k) = V(k)<=N;
N = min(V(k),N);
end
Z = V(X);
Generates this output vector:
>> Z
ans =
1.1000 1.8000 2.5000 4.0000 5.0000 4.2000 2.2000 1.1100
Compared to the requested output:
>> [ 1.1 1.8 2.5 4.0 5.0 4.2 2.2 1.11]
ans =
1.1000 1.8000 2.5000 4.0000 5.0000 4.2000 2.2000 1.1100