2
$\begingroup$

I would like to generate a lower triangular matrix that has checkmarks in the non-zero entries. The way that I created this matrix is the following

test1[x_] := ConstantArray["\[Checkmark]", x]
matrix1[x_] := 
 LowerTriangularize[ArrayReshape[test1[x], {x, x}, "\[Checkmark]"]]

Now, I wanted to have a different colour for the first column. This I did in the following way:

 element = Grid[matrix1[13], Background -> {{Pink}, None}]

I can also put a colour in the second column in the following way:

element2 = Grid[matrix1[13], Background -> {{Pink, Pink}, None}]

I have two questions:

  1. How can I put a different value for the zero entries? Example, instead of zero I would like to have an "x". I imagine that this can be done with an If command (?) or a With but I cannot work around it.

  2. How can I put colour in the non-zero elements of the second column?

$\endgroup$
3
$\begingroup$

For 1.

matrix1[13] /. 0 -> "x"

For 2.

colors = Table[{i, 2} -> If[matrix1[13][[i, 2]] == "\[Checkmark]", Pink, None], {i, 13}];
Grid[matrix1[13] /. 0 -> "x", Background -> {None, None, Flatten@colors}]

enter image description here

| improve this answer | |
$\endgroup$
4
$\begingroup$

You can use Array or SparseArray to define a function that generates an array with desired dimensions and desired entries below and above the main diagonal:

mf1[dims_, a_, b_: 0] := Array[If[# >= #2, a, b] &, dims];

mf1[{13, 13}, "✓", "x"] // Grid

enter image description here

mf2[dims_, a_, b_: 0] := SparseArray[{i_, j_} /; i >= j -> a, dims, b]

mf2[{13, 13}, "✓", "x"] // Grid

same picture

To style selected cells you can use MapAt:

Grid @ MapAt[Item[#, Background -> Pink] &,  mf1[{13, 13}, "✓", "x"], {2 ;;, 2}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ It's a pity that I cannot accept more than one answer. Thanks for your time and the explanation! $\endgroup$ – DiSp0sablE_H3r0 Oct 25 '19 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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