How to find min and max of a matrix?

3 views (last 30 days)
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?

Accepted Answer

Steven Lord
Steven Lord on 29 Jun 2020
Use sortrows. Specify that you want to sort the first column in ascending order and the second in descending order.
  3 Comments
Steven Lord
Steven Lord on 29 Jun 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 on 2 Jul 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!