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
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
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]
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!