I have a matrix such as this

mat ={{6, 5},{5, 4}}

and I want to find the maximum in every column and minimum in every row. After that I have to get the same number of both. I write this

Max /@ Transpose[mat]


Min /@ mat

The results are {6,5} and {5,4}. However I am not sure how I can find that 5 is the same for both. And also it will be good to have all commands to be in one cycle.


1 Answer 1

mat = {{6, 5}, {5, 4}};

Those row/column indices i,j for which the minimal row element is the same as the maximal column element is:

Tuples[{Min /@ mat, Max /@ Transpose[mat]}] //
 Position[ArrayReshape[Differences[#, {0, 1}][[All, 1]], Dimensions[mat]], 0] &

{{1, 2}}

  • $\begingroup$ This code works for row 1 but when I wrote this matt = {{5, 4}, {6, 5}}; the answer is {{1, 4}} but have to be row 2 column 2 . $\endgroup$
    – M.Alexis
    Commented Nov 7, 2018 at 18:16
  • $\begingroup$ @M.Alexis I can't reproduce that. Try wrapping With[{mat = matt}, ....] around the code. $\endgroup$
    – MeMyselfI
    Commented Nov 7, 2018 at 18:19
  • $\begingroup$ I also tried the solution from @MeMyselfI and it works fine for both matrices, as well as something along the lines of Table[RandomInteger[{0, 10}], {i, 4}, {j, 4}] (it might take a bigger matrix or some repetitions to get something other than a blank table). For a way that is faster than Position see here $\endgroup$
    – Titus
    Commented Nov 8, 2018 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.