How to find min and max of a matrix?
조회 수: 3 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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
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."
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!