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

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 comentarios

What if you have two minimum in the same row?
How can that happen, unless he would have integers? Each row will have exactly one element that is the min. Same for the columns. Of course being the min element on a row does not mean that same element is also the min element of the column it is in - in fact, it probably won't be.
He said for example A=rand(50). It's just an example
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.

Iniciar sesión para comentar.

 Respuesta aceptada

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

Más respuestas (1)

rowmin=min(A,[],2);
colmax=max(A,[],1);
locations = bsxfun(@eq,A,rowmin) & bsxfun(@eq,A,colmax);

Categorías

Etiquetas

Preguntada:

el 15 de Dic. de 2013

Comentada:

el 15 de Dic. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by