3
$\begingroup$

This is a follow-on from a similar question here. If I already have a table of data, say

table1 = {{0, 0, 0, 0, 0, 0}, 
 {0, 1, 2, 3, 4, 5}, 
 {0, 2, 4, 6, 8, 10}, 
 {0, 3, 6, 9, 12, 15}, 
 {0, 4, 8, 12, 16, 20}, 
 {0, 5, 10, 15, 20, 25}, 
 {0, 6, 12, 18, 24, 30}, 
 {0, 7, 14, 21, 28, 35}, 
 {0, 8, 16, 24, 32, 40}, 
 {0, 9, 18, 27, 36, 45}, 
 {0, 10, 20, 30, 40, 50}, 
 {0, 11, 22, 33, 44, 55}, 
 {0, 12, 24, 36, 48, 60}, 
 {0, 13, 26, 39, 52, 65}, 
 {0, 14, 28, 42, 56, 70}, 
 {0, 15, 30, 45, 60, 75}}

and I want to present this in TableForm with the rows whose row numbers are prime (i.e., rows 2,3,5...) in bold, how do I do this by applying functions to table1? Perhaps some combination of If, Length, Range and PrimeQ?

Obviously in the above case the table is small enough to do it manually, but I want to be able to do this with any arbitrary pre-existing table of data.

$\endgroup$

5 Answers 5

3
$\begingroup$

Does something like this do what you want?

table1 /. (row : {a_?NumberQ, b_?PrimeQ, c___?NumberQ} :>Map[Highlighted@Style[#, Bold] &, row]) // TableForm  

Hi Richard. Not sure I fully understand what you're trying to achieve, so the following suggestions are a bit of a guess.

table1[[All, 2]] += 1; 
table1 /. (row : {a_?NumberQ, b_?PrimeQ, c___?NumberQ} :> 
Map[Highlighted@Style[#, Bold] &, row]) // TableForm

All I've done here is modify your table1 to add a 'shift' value of 1 to all values in the second column. If you want to leave table1 as-is, you could try the following.

table1 /. (row : {a_?NumberQ, b_?NumberQ, c___?NumberQ} :> 
Map[Highlighted@Style[#, Bold] &, row] /; 
 PrimeQ[b + 1]) // TableForm

This code imposes a condition such that the rewrite rule is invoked only if the value of the number in the second column when added to 1 is Prime.

$\endgroup$
1
  • $\begingroup$ One additional request - should have made this clearer... What if there's an offset? Like if the first row is actually row 0 instead of row 1? Which bit do I change? $\endgroup$ Commented Mar 28, 2019 at 11:39
2
$\begingroup$

Grid has flexible formatting options for table items compared to TableForm. The syntax is also cleaner.

table1 = {{0, 0, 0, 0, 0, 0}, {0, 1, 2, 3, 4, 5}, {0, 2, 4, 6, 8, 
    10}, {0, 3, 6, 9, 12, 15}, {0, 4, 8, 12, 16, 20}, {0, 5, 10, 15, 
    20, 25}, {0, 6, 12, 18, 24, 30}, {0, 7, 14, 21, 28, 35}, {0, 8, 
    16, 24, 32, 40}, {0, 9, 18, 27, 36, 45}, {0, 10, 20, 30, 40, 
    50}, {0, 11, 22, 33, 44, 55}, {0, 12, 24, 36, 48, 60}, {0, 13, 26,
     39, 52, 65}, {0, 14, 28, 42, 56, 70}, {0, 15, 30, 45, 60, 75}};


rows = Flatten@Position[table1[[All, 2]], _?PrimeQ];
s1 = Thread[rows -> Directive[Bold, Black]];
b2 = Thread[rows -> Lighter@Cyan];
b3 = {{9, 11}, {2, 4}} -> Lighter@Yellow;

Grid[table1
 , Background -> {None, b2, b3}
 , ItemStyle -> {Automatic, s1 }
 , Frame -> {None, None, {{9, 11}, {2, 4}} -> Directive[Red]}
 ]

enter image description here

$\endgroup$
1
$\begingroup$
list = 
 {{0, 0, 0, 0, 0, 0}, 
 {0, 1, 2, 3, 4, 5}, 
 {0, 2, 4, 6, 8, 10}, 
 {0, 3, 6, 9, 12, 15}, 
 {0, 4, 8, 12, 16, 20}, 
 {0, 5, 10, 15, 20, 25}, 
 {0, 6, 12, 18, 24, 30}, 
 {0, 7, 14, 21, 28, 35}, 
 {0, 8, 16, 24, 32, 40}, 
 {0, 9, 18, 27, 36, 45}, 
 {0, 10, 20, 30, 40, 50}, 
 {0, 11, 22, 33, 44, 55}, 
 {0, 12, 24, 36, 48, 60}, 
 {0, 13, 26, 39, 52, 65}, 
 {0, 14, 28, 42, 56, 70}, 
 {0, 15, 30, 45, 60, 75}}

Get positions of primes in 2nd column with appended All

p = Append[All] /@ Position[list, {_, _?PrimeQ, __}]

{{3, All}, {4, All}, {6, All}, {8, All}, {12, All}, {14, All}}

Using ReplaceAt (new in 13.1)

ReplaceAt[a_ :> Style[a, Bold], p] @ list // TableForm

enter image description here

$\endgroup$
1
$\begingroup$
Dataset[table1, 
 ItemStyle -> _?(PrimeQ[# - 1] &) -> Bold, 
 Background -> (If[PrimeQ[#2[[1]] - 1], Cyan, White] &)]

enter image description here

$\endgroup$
0
$\begingroup$

Since "row number" is not clearly specified in the OP, here I just assume that the first row ({0, 0, 0, 0, 0, 0}) has row number 1.

Using MapIndexed:

ClearAll[style]
style[list_List, {n_}] /; PrimeQ[n] := Style[#, Bold] & /@ list
style[list_List, {n_}] := list

MapIndexed[style, table1] // TableForm

or MapAt

MapAt[Style[#, Bold] &, table1, 
  Table[{Prime[row], All}, {row, 1, 
    PrimePi[Length@table1]}]] // TableForm

enter image description here

$\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.