How to find min and max of a matrix?

3 visualizaciones (últimos 30 días)
Kerstin Olsson
Kerstin Olsson el 29 de Jun. de 2020
Comentada: Kerstin Olsson el 2 de Jul. de 2020
Hi,
Suppose I have a matrix with two columns:
A= [1.6700 0.5300; 1.6800 0.5400; 1.6900 0.5200; 1.6500 0.5100; 1.7000 0.5500];
And I'm looking for the row which simulaneously gives me the smallest value of the first column and the largest value of the second column, how would I go about doing that?
I'm not looking for the min of the first column and the max of the second column separately. I'm looking for the "best combination", i.e. the single row which gives me the smallest possible value of the first column and the largest possible column of the second column.
Any ideas?

Respuesta aceptada

Steven Lord
Steven Lord el 29 de Jun. de 2020
Use sortrows. Specify that you want to sort the first column in ascending order and the second in descending order.
  3 comentarios
Steven Lord
Steven Lord el 29 de Jun. de 2020
>> rng(1, 'twister');
>> A = randi(10, [20 2]);
>> B = sortrows(A, [1 2], {'ascend', 'descend'})
The first column of the first row of B has the smallest value among all values in the first column of A. Of all the rows in A that have that value in the first column, the second element in the first row of B has the largest value in the second column of that subset of rows in A.
If that's not what you meant by "best combination" please clarify exactly what criteria you're using to define "best."
Kerstin Olsson
Kerstin Olsson el 2 de Jul. de 2020
To get the best combiation I decided to rewrite the smallest and largets values as deviations from a set target for each. Then I summed those deviations and sorted the sum to find the smallest value.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by