# How to construct this matrix in Mathematica

I create a symbolic matrix using the following:

mat = ToExpression[Table[StringJoin[{"s", ToString[i], ToString[j]}], {i, 1, d}, {j, 1, d}]]


for arbitrary $$d$$. Notice that for $$d=2$$, this gives

$$\begin{pmatrix} s11 & s12 \\ s21 &s22\end{pmatrix}.$$

I do this since an analytic expression is important. However, I would like to get numerics. How can I update the above define for mat to get a function of the form:

matNEW[s11_, s12_, s21_, s22_]


such that I can evaluate the matrix later in the script for specific values of the parameters.

• Start by using indexed variables instead of constructed expressions, such as s[1, 2] etc. Then making your array is simple: mat[d_Integer] := Array[s, {d, d}]. Later on, use replacements on your mat: mat[2] /. {s[1, 1] -> 2, ...} perhaps. – MarcoB May 29 at 16:09

You may use Indexed with Array to create a matrix that can be switched between symbolic and numeric content.

To input the script capital R symbol enter: Esc scR Esc . See more at Notational Alphabet Characters.

indexedArray[m_Symbol, dims_?(VectorQ[#, # ∈ PositiveIntegers &] &)] :=
Array[Indexed[m, {##}] &, dims]


indexedArray builds a symbolic matrix on the unassigned symbol provided.

ℛ = indexedArray[r, {3, 2}];
MatrixForm@ℛ


### For symbolic calculations:

Transpose@ℛ // MatrixForm


𝓒 = indexedArray[c, {2, 3}];
𝓒.ℛ // MatrixForm


### For numeric calculations:

We need to assign values to r not ℛ.

r = Partition[Range@6, 2];
MatrixForm@ℛ


{Range@3}.ℛ // MatrixForm


𝓒.ℛ // MatrixForm


Clear the Indexed variable to return to symbolic calculations.

r =.
ℛ // MatrixForm


Hope this helps.

• @MarcoB Thanks for the script formatting. I did not realise that the Mma.SE Editor Tools worked on script symbols. Is this from a recent update? – Edmund May 30 at 0:45
• You’re right, the editor doesn’t. I used steampiano.net/msc , which got the script R, and finally I copy-pasted the script C from a wiki page :-) – MarcoB May 30 at 2:24
• @MarcoB No, it does do it now. I tried it before posting the comment. It didn't when I first used it a long time ago but it does do scripts now. – Edmund May 30 at 2:27
• Well that’s interesting! You’re right, it worked. I wonder what I did wrong the first time. Even better then! – MarcoB May 30 at 4:20
• @Edmund, great answer. Couple of questions. 1. How does indexedArray set the indices of r as subscripts? 2. Also, how does r = Partition[Range@6, 2]; assign values to the values of r? Can we instead set the values as a function of their indices? i.e. rjk = f[j,k] for some defined function f. – Sid May 31 at 21:46