How I can find in a big matrix the elements that are simultaneously min on rows and max on columns?
1 view (last 30 days)
Show older comments
Sebastian Ciuban
on 15 Dec 2013
Commented: Image Analyst
on 15 Dec 2013
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 Comments
Image Analyst
on 15 Dec 2013
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.
Accepted Answer
Image Analyst
on 15 Dec 2013
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
0 Comments
More Answers (1)
Matt J
on 15 Dec 2013
rowmin=min(A,[],2);
colmax=max(A,[],1);
locations = bsxfun(@eq,A,rowmin) & bsxfun(@eq,A,colmax);
0 Comments
See Also
Categories
Find more on String Parsing 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!