How to select two data of minimum above and below

1 次查看(过去 30 天)
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 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by