# How to replace the number of each element of a matrix with a subscript?

I want to get the matrix as Figure 2. There should be two ways to achieve the result.

1. There is a matrix as Figure 1, and change it to the form of Figure 2.

2. Generating the matrix as Figure 2 directly.

How can I do it? Thanks.

Note：The matrix in Figure 2 is a symmetric matrix

Figure 1

Figure 2

Codes

{{C11,C12,C13,C14,C15,C16},{0,C22,C23,C24,C25,C26},{0,0,C33,C34,C35,C36},{0,0,0,C44,C45,C46},{0,0,0,0,C55,C56},{0,0,0,0,0,C66}}


Clear["Global*"]


Use an indexed variable and you can easily generate the result without the original matrix. The variable C is used by Mathematica so use c instead. You can display c as C if desired.

Format[c[m_, n_]] := Subscript[C, 10 m + n]


(EDIT: Or as suggested by LouisB, for larger arrays use Format[c[m_, n_]] := Subscript[C, Row[{m, n}, " "]])

(mat = Array[If[#1 <= #2, c[##], 0] &, {6, 6}]) // MatrixForm


(mat2 = Array[If[#1 <= #2, c[##], c[#2, #1]] &, {6, 6}]) // MatrixForm


• When the subscripts are not single digits, a handy format is Format[c[m_, n_]] := Subscript[C, Row[{m, n}, " "]], which puts a little space between the subscripts. Jul 12 at 6:37
• @LouisB - Thanks. Added edit above Jul 12 at 12:54

Try this

Table[If[row>col,Subscript[c,col,row],Subscript[c,row,col]],{row,1,6},{col,1,6}]


and if you need that formatted to look at then

%//MatrixForm


As requested, remove the commas

Table[If[row>col,Subscript[c,10*col+row],Subscript[c,10*row+col]],{row,1,6},{col,1,6}]


but that will break if you have more than 9 columns and rows. You could then try things like ToString and StringJoin or maybe come up with your own ideas or how to glue together the subscript values.

As requested, try again to turn matrix1 into matrix 2

matrix1={
{C11,C12,C13,C14,C15,C16},{0,C22,C23,C24,C25,C26},{0,0,C33,C34,C35,C36},
{0,0,0,C44,C45,C46},{0,0,0,0,C55,C56},{0,0,0,0,0,C66}};
matrix2=Table[If[row>col,
Subscript[c,ToExpression[StringDrop[ToString[matrix1[[col,row]]],1]]],
Subscript[c,ToExpression[StringDrop[ToString[matrix1[[row,col]]],1]]]],
{row,1,6},{col,1,6}]


Check all this very carefully to make certain that it is correct

• Thanks. Can you generate a matrix without a comma in subscripts? and how to change the matrix in figure 1 to that in Figure 2? Jul 11 at 8:12
• Thank you. In fact, transforming matrix1 to matrix2 is more difficult than generating a new matrix2 directly, because it involves in becoming numbers to subscripts. Can you show the process of transformation? Jul 11 at 9:47
m0 = UpperTriangularize @ Array[Symbol @ StringRiffle[{"C", ##}, ""] &, {6, 6}];

TeXForm @ MatrixForm @ m0


$$\left( \begin{array}{cccccc} \text{C11} & \text{C12} & \text{C13} & \text{C14} & \text{C15} & \text{C16} \\ 0 & \text{C22} & \text{C23} & \text{C24} & \text{C25} & \text{C26} \\ 0 & 0 & \text{C33} & \text{C34} & \text{C35} & \text{C36} \\ 0 & 0 & 0 & \text{C44} & \text{C45} & \text{C46} \\ 0 & 0 & 0 & 0 & \text{C55} & \text{C56} \\ 0 & 0 & 0 & 0 & 0 & \text{C66} \\ \end{array} \right)$$

1.

rule = ReplaceAll[s_Symbol :>
(Subscript @@ (ToExpression @ StringTake[SymbolName @ s, #] & /@ {1, -2}))];

m0b = # + Transpose[UpperTriangularize[#, 1]] & @ Map[rule, m0, {2}];

TeXForm @ MatrixForm @ m0b


$$\left( \begin{array}{cccccc} C_{11} & C_{12} & C_{13} & C_{14} & C_{15} & C_{16} \\ C_{12} & C_{22} & C_{23} & C_{24} & C_{25} & C_{26} \\ C_{13} & C_{23} & C_{33} & C_{34} & C_{35} & C_{36} \\ C_{14} & C_{24} & C_{34} & C_{44} & C_{45} & C_{46} \\ C_{15} & C_{25} & C_{35} & C_{45} & C_{55} & C_{56} \\ C_{16} & C_{26} & C_{36} & C_{46} & C_{56} & C_{66} \\ \end{array} \right)$$

2.

m1 = Array[Subscript[C, Row @ Sort @ {##}] &, {6, 6}];

TeXForm @ MatrixForm @ m1


$$\left( \begin{array}{cccccc} C_{11} & C_{12} & C_{13} & C_{14} & C_{15} & C_{16} \\ C_{12} & C_{22} & C_{23} & C_{24} & C_{25} & C_{26} \\ C_{13} & C_{23} & C_{33} & C_{34} & C_{35} & C_{36} \\ C_{14} & C_{24} & C_{34} & C_{44} & C_{45} & C_{46} \\ C_{15} & C_{25} & C_{35} & C_{45} & C_{55} & C_{56} \\ C_{16} & C_{26} & C_{36} & C_{46} & C_{56} & C_{66} \\ \end{array} \right)$$

m2 = UpperTriangularize @ m1;

TeXForm @ MatrixForm @ m2


$$\left( \begin{array}{cccccc} C_{11} & C_{12} & C_{13} & C_{14} & C_{15} & C_{16} \\ 0 & C_{22} & C_{23} & C_{24} & C_{25} & C_{26} \\ 0 & 0 & C_{33} & C_{34} & C_{35} & C_{36} \\ 0 & 0 & 0 & C_{44} & C_{45} & C_{46} \\ 0 & 0 & 0 & 0 & C_{55} & C_{56} \\ 0 & 0 & 0 & 0 & 0 & C_{66} \\ \end{array} \right)$$

• Thanks. But the result of m1 can not be copied to MS Word, taking the $C_{16}$ in m1 for example, it shows as Subscript[C, Row[{1, 3}]] instead of Subscript[C, 13], so can not be used in word as an equation. Why and how to overcome it? Jul 14 at 7:52
• @likehust, try replacing Row @ Sort @ {##} with StringRiffle[Sort@{##}, ""]`.
– kglr
Jul 14 at 14:09