How I can find in a big matrix the elements that are simultaneously min on rows and max on columns?

조회 수: 1 (최근 30일)
For example I have
A=rand(50,50);
And I want to find the position and the values that are simultaneously min on rows and max on columns
  댓글 수: 4
Image Analyst
Image Analyst 2013년 12월 15일
Exactly. So there will never be two minimums in the same row. And, for a given row, whatever column the row min does occur in will only be the max of the column it's in 1 time in 50. So most of the time he won't find any elements meeting the criteria. Perhaps you and I are interpreting what he said differently.

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

채택된 답변

Image Analyst
Image Analyst 2013년 12월 15일
This is very unlikely for a 5 by 5. I decreased the matrix to a 4 by 3 and it happens sometimes. Here's the code:
A = randn(4, 3)
[rowMins,colIndexOfMins] = min(A,[],2) % Within rows
[colMaxs, rowIndexOfMaxs] = max(A,[],1) % Within columns
rowMinMap = ismember(A, rowMins)
colMaxMap = ismember(A, colMaxs)
itsBoth = rowMinMap & colMaxMap
% Tell if there are any
if any(itsBoth(:))
[rows, cols] = find(itsBoth);
for k = 1 : length(rows)
message = sprintf('Row %d, column %d is both a row min and a column max',...
rows(k), cols(k));
helpdlg(message);
end
else
message = 'No element is both a row min and a column max';
warndlg(message);
end
In the command window for one case where it found such an element:
A =
-0.636128249660041 0.777003526719788 1.04858076053644
0.317851419059697 0.622393924172013 0.660707086367175
0.138047974449526 0.647380884516047 2.50877247318511
-0.710735074811226 -0.425631681660351 1.06345963904102
rowMins =
-0.636128249660041
0.317851419059697
0.138047974449526
-0.710735074811226
colIndexOfMins =
1
1
1
1
colMaxs =
0.317851419059697 0.777003526719788 2.50877247318511
rowIndexOfMaxs =
2 1 3
rowMinMap =
1 0 0
1 0 0
1 0 0
1 0 0
colMaxMap =
0 1 0
1 0 0
0 0 1
0 0 0
itsBoth =
0 0 0
1 0 0
0 0 0
0 0 0

추가 답변 (1개)

Matt J
Matt J 2013년 12월 15일
rowmin=min(A,[],2);
colmax=max(A,[],1);
locations = bsxfun(@eq,A,rowmin) & bsxfun(@eq,A,colmax);

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by