How to find min and max of a matrix?

조회 수: 3 (최근 30일)
Kerstin Olsson
Kerstin Olsson 2020년 6월 29일
댓글: Kerstin Olsson 2020년 7월 2일
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?

채택된 답변

Steven Lord
Steven Lord 2020년 6월 29일
Use sortrows. Specify that you want to sort the first column in ascending order and the second in descending order.
  댓글 수: 3
Steven Lord
Steven Lord 2020년 6월 29일
>> 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 2020년 7월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by