# How can I find the location of the minimum and maximum

66 ビュー (過去 30 日間)
Tony Montgomery 2014 年 10 月 31 日
コメント済み: Matt Tearle 2014 年 10 月 31 日
I am to test some ill condition matrices, so I want create a loop that creates 5 random nxn matrices. Then I want to be able to find the matrix with the minimum condition number, and the one with the maximum condition number. I have the following code:
for i=1:5
Ai = rand(n,n);
C(i) = cond(Ai);
end
max(C)
min(C)
So I was able to find the min and max values, but I want to know which matrix they are associated with. If i knew where they occurred I can figure it out, but how can I find WHERE the min and max occured??

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

### 回答 (3 件)

SK 2014 年 10 月 31 日

[maxvalue, index_of_max] = max(C);
[minvalue, index_of_min] = min(C);
##### 0 件のコメント表示 -1 件の古いコメント非表示 -1 件の古いコメント

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

Orion 2014 年 10 月 31 日

Hi,
just use the second output argument of the min and max functions.
n=10;
for i=1:5
Ai = rand(n,n);
C(i) = cond(Ai);
end
[MaxVal,MaxIndex] = max(C);
[MinVal,MinIndex] = min(C);
% check
disp([C(MaxIndex),MaxVal])
disp([C(MinIndex),MinVal])
##### 0 件のコメント表示 -1 件の古いコメント非表示 -1 件の古いコメント

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

Matt Tearle 2014 年 10 月 31 日
Others have explained how to get the index of the max or min, but I'm wondering if you actually want to know what the matrix is that it's associated with. Currently you overwrite your matrix each time through the loop, so there's no way to retrieve it. So
Ai = rand(n,n,5);
C = zeros(5,1);
for i = 1:5
C(i) = cond(Ai(:,:,i));
end
[idx,maxC] = max(C);
maxA = Ai(:,:,idx);
##### 2 件のコメント表示 1 件の古いコメント非表示 1 件の古いコメント
Matt Tearle 2014 年 10 月 31 日
That's what my code does -- it keeps all the matrices in a 3-D array (n-by-n-by-5). Then once you have idx -- the index of the maximum -- you can index into Ai to get the idx element: maxA = Ai(:,:,idx);

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

### カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!