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.

1 view (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 9 Aug 2017
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

More Answers (1)

Image Analyst
Image Analyst on 9 Aug 2017
1.8 is not an increment of 1.1 above 1.1, and 2.5 is not 1.1 above 1.8. And you can't add any number of 1.1's to 1.1 to get exactly 5. So I'm not sure what rule you're using but it's not [1.1, 2.2, 3.3, 4.4, 5.5, ...] etc.
Anyway, find the max, then use the colon operator:
m = [ 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]
maxValue = max(m)
% Create vector increasing from 1.1 to the max in steps of 1.1,
% then going down from the max to 1.11 in steps of -0.5:
m2 = [1.1 : 1.1 : maxValue, (maxValue - 0.5) : -0.5 : 1.11]
  1 Comment
Olumide Omotere
Olumide Omotere on 9 Aug 2017
Thank you image analyst for your response but the outcome is different.The expected outcome is [1.1 1.8 2.5 5.0 4.2 3.3 2.5]the code should pick data points monotonically increasing before the peak and monotonically decreasing after the peak

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!