2
$\begingroup$
TableForm[Table[i/j + 4*Boole[j > i] // N, {i, 3}, {j, 4}], TableHeadings -> {{"Row1", "Row2", "Row3"}, {"Col1", "Col2", "Col3", "Col4"}}]

Produces the following table:

enter image description here

How select the maximum value in each row and make it a bold font?

So that 4.5, 4.66667 and 4.75 are bold in the 1st, 2nd and 3rd row.

Thanks

$\endgroup$
2

3 Answers 3

2
$\begingroup$
   TableForm[ (m = Max[#];   (#  /. m :> Style[m, Red] ) ) & /@ 
        Table[i/j + 4*Boole[j > i] // N, {i, 3}, {j, 4}] ,
        TableHeadings -> {{"Row1", "Row2", "Row3"}, {"Col1", "Col2", "Col3", "Col4"}}]

enter image description here

Red is easier to see but Bold works as well..

$\endgroup$
2
  • $\begingroup$ Very nice, Thanks! $\endgroup$ Jan 13, 2015 at 22:10
  • $\begingroup$ Yes, red is better. But I am think I am going to export the table for publishing. So I might avoid color. $\endgroup$ Jan 13, 2015 at 22:11
4
$\begingroup$

Yet another realization of the straightforward idea

t = Table[i/j + 4*Boole[j > i] // N, {i, 3}, {j, 4}];
h = {{"Row1", "Row2", "Row3"}, {"Col1", "Col2", "Col3", "Col4"}};

Style[TableForm[# /. x : Max@# :> Style[x, Bold] & /@ t, 
  TableHeadings -> h], FontFamily -> "Times"]

enter image description here

I change FontFamily for better visibility

$\endgroup$
2
  • 1
    $\begingroup$ i knew there was a cleaner way to do that.. $\endgroup$
    – george2079
    Jan 13, 2015 at 23:50
  • $\begingroup$ Is there a way to get the same column-wise instead of row-wise? $\endgroup$
    – Karl A
    Apr 18, 2022 at 5:59
1
$\begingroup$

Using Position with MapAt:

ClearAll[sF];
sF = With[{pos = Thread[{Range@Length@#, Position[#, Max@#][[1, 1]] & /@ #}]},
          MapAt[Style[#, Bold, Red] &, #, pos]] &;

t = Table[i/j + 4*Boole[j > i] // N, {i, 3}, {j, 4}];
h = {{"Row1", "Row2", "Row3"}, {"Col1", "Col2", "Col3", "Col4"}};
TableForm[sF@t, TableHeadings -> h] 

enter image description here

... with ReplacePart:

ClearAll[sF2];
sF2 = With[{m=#, pos = Thread[{Range@Length@#, Position[#, Max@#][[1, 1]] & /@ #}]},
           ReplacePart[m, (# -> Style[m[[## & @@ #]], Bold, Red]) & /@ pos]] &;

TableForm[sF2@t, TableHeadings -> h]
(* same picture *)

... with Part assignment:

ClearAll[sF3];
sF3 = Module[{r=#, p = Position[#, Max@#][[1]]},
             (r[[#]] = Style[r[[#]], Bold, Red]) & /@ p; r] & /@ # &;

TableForm[sF3@t, TableHeadings -> h]
(* same picture *)
$\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.