How to select two data of minimum above and below
1 view (last 30 days)
Show older comments
Mekala balaji
on 16 Sep 2017
Answered: Star Strider
on 16 Sep 2017
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
0 Comments
Accepted Answer
Star Strider
on 16 Sep 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!