Checking if minimum of a matrix occurs on the diagonal

조회 수: 4 (최근 30일)
KAE
KAE 2018년 10월 4일
편집: KAE 2018년 10월 4일
For a matrix, I would like to check if the minimum value of each row is found on the diagonal. The minimum value can occur more than once within a row. Since the min command returns the index of the first occurrence of the minimum, min can miss later occurrences on the diagonal, as follows:
testMatrix = [ 1 3 5 1
2 0 4 6
3 9 2 7
-2 9 4 -2];
[~, iMin] = min(a)
iMin =
4 2 3 4
How can I check if the row minimum is found on the diagonal? I am working with a very large matrix in reality, so I would like to avoid looping down the rows.
  댓글 수: 2
Adam
Adam 2018년 10월 4일
편집: Adam 2018년 10월 4일
Can you not do something like
diag( testMatrix ) == min( testMatrix )
?
I haven't put a whole lot of thought into it so there may be perfectly good reasons why you can't!
KAE
KAE 2018년 10월 4일
This does not seem to check the diagonals, or maybe I am misunderstanding,
ans =
4×4 logical array
0 0 0 0
0 1 0 0
0 0 1 0
1 0 0 1

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

채택된 답변

Julian Hapke
Julian Hapke 2018년 10월 4일
with a slight modification of Adams comment:
diag(testMatrix) == min(testMatrix , [], 2)
min accepts the dimension in which you want the minimum, in this case you want the minimum of each row, so in direction 2 and check against the diagonal of the matrix
  댓글 수: 1
KAE
KAE 2018년 10월 4일
편집: KAE 2018년 10월 4일
Works and returns a vector saying whether it is on the diagonal or not, which I realized is what I was looking for when I saw the output. Thanks!
Here it shows it returns zero for rows when the minimum is not on the diagonal.
testMatrixOneWrong = ...
[ 1 3 5 -2
2 0 4 6
3 9 2 7
-2 9 4 -2]
diag(testMatrixOneWrong) == min(testMatrixOneWrong, [], 2)
ans =
4×1 logical array
0
1
1
1

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by