how can I get the min and corresponding max in matrix?

2 ビュー (過去 30 日間)
JacobM
JacobM 2016 年 9 月 26 日
コメント済み: Walter Roberson 2016 年 9 月 26 日
I want to find the max in a matrix r*2 where r=1 up to 10 depends on the input from the user. and then return the min corresponding element in that row to the max.
Ex.
x=[5 2;4 1;1 3];
x_max1=max(x(:,1)); x_min1=min(x(:,1));
x_max2=max(x(:,2)); x_min2=min(x(:,2)); % for second column
x_max=max(x_max1,x_max2); x_min=min(x_min1,x_min2);
this code will return, max as 5 and min as 1 while I want the code to return the max which is 5 and the corresponding element in that row which is 2'? Also, if there is another way to simplify the code will be also helpful

採用された回答

Image Analyst
Image Analyst 2016 年 9 月 26 日
Try this:
x=[5 2;
4 1;
1 3];
% Get max in either column
maxValue = max(x(:))
% Find out what row and column it apepars at.
[row, column] = find(x == maxValue)
% Get the min value at that row.
minValue = min(x(row, :))
  2 件のコメント
JacobM
JacobM 2016 年 9 月 26 日
編集済み: Walter Roberson 2016 年 9 月 26 日
works perfect! but when I have another max it will return two values while I want only one value.
Ex. x=[5 2;4 1;1 5];
the max is 5 and this code will return both corresponding values to 5, which are 2 and 1. can I just get the min one which is 1 and let the code ignore the other value? and thanks for your input
Walter Roberson
Walter Roberson 2016 年 9 月 26 日
min() the result.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by