0
$\begingroup$

I have this code

mat = ( {
    {7, 6},
    {2, 4}
   } );
Print[" Min ", 
 Grid[{Min /@ mat}, 
  Background -> {Automatic, 
    Automatic, {{1, 1} -> Red, {1, 2} -> Red}}, ItemSize -> {2, 1}, 
  Frame -> All]]

However it displays only two cells that are the minimum of each row. How can I make the other numbers appear in the table, but only the minimum to be in red.

$\endgroup$

2 Answers 2

0
$\begingroup$

Like this?

Row@{
  "Min ",
  Grid[
   mat,
   Background -> {
     Automatic,
     Automatic,
     Table[
      {n, First@Ordering[mat[[n]]]} -> Red,
      {n, Length@mat}
      ]
     },
   Frame -> All,
   ItemSize -> {2, 1}
   ]
  }

enter image description here

We build the list of rules for the red cells by going over each row using Table. The index of the minimal element of each row can be found using Ordering

Here the same thing for the maximum in each column:

Row@{
  "Max ",
  Grid[
   mat,
   Background -> {
     Automatic,
     Automatic,
     Table[
      {Last@Ordering[mat[[All, n]]], n} -> Green,
      {n, Length@mat[[1]]}
      ]
     },
   Frame -> All,
   ItemSize -> {2, 1}
   ]
  }

enter image description here

A few things changed:

  • We iterate over the columns of the matrix now.
    • Therefore, Table goes over the length in the second dimension.
    • We need to look at the elements per column, so mat[[All,n]] instead of mat[[n]].
    • The indices of the position specifications were also flipped
  • Since we're looking for the maximum, we take the Last element of the list returned by Ordering.
$\endgroup$
2
  • $\begingroup$ Thank you, by finding the maximum in each pillar what should I replace?I think row with column,but I am not sure for the others. $\endgroup$
    – M.Alexis
    Commented Jan 3, 2019 at 18:34
  • $\begingroup$ @M.Alexis I've added a second example $\endgroup$
    – Lukas Lang
    Commented Jan 3, 2019 at 18:54
1
$\begingroup$

This is one way:

{{7, 6}, {2, 4}} //
  ReplaceAll[v_?VectorQ :> (v /. Min[v] -> Item[Min[v], Background -> Red])] // 
Legended[Grid[#, Dividers -> All], SwatchLegend[{Red}, {"minimum"}]] &

Mathematica graphics

$\endgroup$

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.