How to select two data of minimum above and below

1 回表示 (過去 30 日間)
Mekala balaji
Mekala balaji 2017 年 9 月 16 日
回答済み: Star Strider 2017 年 9 月 16 日
Hi,
I have data as below:
1.2 4.3 0.2 9
2.7 4.6 1.3 11
2.7 2.2 4.6 2
0.2 4.1 3.0 12
0.4 5.3 2.6 1
1.1 4.2 2.3 5
2.3 4.0 2.0 3
2.3 4.0 2.0 10
Given point is 12. Based on 4th column, I want to select two data above 12, which are minimum values in column 4, two data below 12 (which are minimum, in column 4). My desired output as below:
1.2 4.3 0.2 9
2.7 2.2 4.6 2
0.2 4.1 3.0 12
0.4 5.3 2.6 1
2.3 4.0 2.0 3

採用された回答

Star Strider
Star Strider 2017 年 9 月 16 日
This works:
M = [1.2 4.3 0.2 9
2.7 4.6 1.3 11
2.7 2.2 4.6 2
0.2 4.1 3.0 12
0.4 5.3 2.6 1
1.1 4.2 2.3 5
2.3 4.0 2.0 3
2.3 4.0 2.0 10];
M12 = find(M(:,4) == 12); % Index Of Row With ‘12’ In Column #4
Ma12 = sortrows(M(1:(M12-1),:),4); % Sorted Rows Above
Mb12 = sortrows(M((M12+1):end,:),4); % Sorted Rows Below
Result = [Ma12(1:2,:); M(M12,:); Mb12(1:2,:)] % Desired Result
Result =
2.7 2.2 4.6 2
1.2 4.3 0.2 9
0.2 4.1 3 12
0.4 5.3 2.6 1
2.3 4 2 3

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by