How to select two data of minimum above and below
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mekala balaji
am 16 Sep. 2017
Beantwortet: Star Strider
am 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 Kommentare
Akzeptierte Antwort
Star Strider
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!