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 (최근 30일)
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.

채택된 답변

Stephen23
Stephen23 2017년 8월 9일
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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 8월 9일
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
Olumide Omotere
Olumide Omotere 2017년 8월 9일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by