-1
$\begingroup$

I was wondering if it's possible to colour a column or a row in a matrix.

For example is it possible to add colour for one of the rows/columns of the following matrixform code?

MatrixForm[{{n, -1 + n, -2 + n, ⋯, 1}, {2 n, 2 n - 1, 
 2 n - 2, ⋯, n + 1}, {3 n, 3 n - 1, 3 n - 2, ⋯, 
 2 n + 1},  {⋮, ⋮, ⋮, ⋱, ⋮}, {n^2, -1 + n^2, -2 + n^2,⋯, HoldForm[(n - 1) n + 1]}}]

I would appreciate any help or suggestion.

EDIT: For example what if we want to colour only one column in the following matrix?

Rasterize@
Style[MatrixForm[{{n, -1 + n, -2 + n, ⋯, 1}, {2 n, 2 n - 1, 
 2 n - 2, ⋯, n + 1}, {3 n, 3 n - 1, 3 n - 2, ⋯, 
 2 n + 1},  {⋮, ⋮, ⋮, ⋱, ⋮}, {n^2, -1 + n^2, -2 + n^2,⋯, HoldForm[(n - 1) n + 1]}}, 
TableAlignments -> {Center, Center}] // TraditionalForm, 
Background -> LightBlue, FontFamily -> Times, 30]

enter image description here

EDIT: Thanks to @Kuba, @E.Doroskevic and @Mr. Wizard I managed to get the following matrix:

Grid[Table[{{1, 2, 3, ⋯, 10}, {2, 3, 4, ⋯, 11}, {3, 4, 5, ⋯, 12}, 
   {⋮, ⋮, ⋮, ⋱, ⋮}, {10, 11, 12, ⋯, 20}}], Background ->{{LightBlue},None}, 
   ItemStyle -> {{Red, Blue, Green, Black}}, ItemSize -> {5, 5}]

enter image description here

Also how can I present the above as a matrix with parentheses or square brackets?

I have tried \\MatrixForm but it didn't work.

$\endgroup$
11
  • 1
    $\begingroup$ You can use MapIndexed[] to apply Style[] to any particular row, column, or entry. $\endgroup$ – J. M.'s ennui Nov 26 '15 at 14:41
  • $\begingroup$ @J.M. Could you kindly show me a simple example? Thank you. $\endgroup$ – johnny09 Nov 26 '15 at 14:54
  • 1
    $\begingroup$ Why not experiment? I already pointed out the functions you'll need to use; look up how they're used in the docs, and try things out. If you get stuck, edit your question to include what you tried with these, and we'll take it from there. $\endgroup$ – J. M.'s ennui Nov 26 '15 at 15:10
  • 3
    $\begingroup$ Grid has Background option which quite wide documentation. Take a look there. $\endgroup$ – Kuba Nov 26 '15 at 15:31
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/q/31152/131 $\endgroup$ – Yves Klett Nov 26 '15 at 16:31
4
$\begingroup$

Disclaimer:
Taken from documentation

Code:

Grid[Table[x, {4}, {7}], Background -> {{Pink}, None}]

Output:

output

Reference:
Grid

P.S
Most specifically look at the documentation provided for the associated option Background

$\endgroup$
5
  • $\begingroup$ Thanks for your answer. But how can I present the required output as a matrix with parentheses? $\endgroup$ – johnny09 Nov 26 '15 at 17:15
  • 1
    $\begingroup$ @johnny09 wrap the code above with { }? Is that what you mean? $\endgroup$ – e.doroskevic Nov 26 '15 at 17:17
  • $\begingroup$ Not exactly. What I want is to create a matrix. So I need parentheses or square brackets to the left and right of the entries. $\endgroup$ – johnny09 Nov 26 '15 at 17:23
  • 2
    $\begingroup$ @johnny09 I would advise you to refer to the reference left above in my answer. However, based on my comprehension of what you are aiming to achieve, following (simplified) sample should suffice as a demonstration: {Grid[{{x, x, x}, {x, x, x}}, Background -> {{Pink}, None}]} $\endgroup$ – e.doroskevic Nov 26 '15 at 17:31
  • 1
    $\begingroup$ maybe {Grid[Table[x, {4}, {7}], Background -> {{Pink}, None}]} // MatrixForm // Rasterize? $\endgroup$ – kglr Nov 26 '15 at 21:31

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