How to select two data of minimum above and below
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mekala balaji
el 16 de Sept. de 2017
Respondida: Star Strider
el 16 de Sept. de 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 comentarios
Respuesta aceptada
Star Strider
el 16 de Sept. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!