# Creating a list of arrays according to given rules

I want to create a list of matrices from the following rules:

1. All matrices of the list have diagonal equal to zero.

2. The first element of the list is a 2x2 matrix, having elements are given by

S2 = SparseArray[{{1, 2} -> 1, {2, 1} -> 0}]


The matrix is {{0, 1}, {0, 0}};

3. The next element of the list is a 3x3 matrix. The elements in this matrix are taken from the elements of S2 with the addition of Subscript[A, 1, 3], Subscript[A, 2, 3], Subscript[A, 3, 1] and Subscript[A, 3, 2].

The matrix is given by

S3 =
{{0, 1, Subscript[A, 1, 3]},
{0, 0, Subscript[A, 3, 3]},
{Subscript[A, 3, 1], Subscript[A, 3, 2],0}}

4. The third element of the list is a 4x4 matrix called S4. The elements of S4 are those from S3 with additional elements Subscript[B, 1, 4], Subscript[B, 2, 4], Subscript[B, 3, 4], Subscript[B, 4, 1], Subscript[B, 4, 2], Subscript[B, 3, 3]. The matrix is given by

S4 =
{{0, 1, Subscript[A, 1, 3], Subscript[B, 1, 4]},
{0, 0, Subscript[A, 2, 3], Subscript[B, 2, 4]},
{Subscript[A, 3, 1], Subscript[A, 3, 2], 0, Subscript[B, 3, 4]},
{Subscript[B, 4, 1], Subscript[B, 4, 2], Subscript[B, 4, 3], 0}}


and so on ....

I would like to use this process to build a list of 25 elements, corresponding the letters A – Z. I am thinking along the lines of

Table[SparseArray[{{i_, i_} -> 0, {i_, j_} -> f[i, j]}, {n, n}], {n, 25}],


but I have not found a function f[i, j] that does what I want.

• I couldn't understand what exactly your rules are, but it seems that you do. Why not construct the matrix manually, then do ArrayRules[SparseArray[yourMatrix]] to discover the rules? – yohbs Jun 2 '17 at 16:15
• Also, you'll run into problems with many uppercase letters that have a reserved meaning, like C, D, E, I, K and so on – yohbs Jun 2 '17 at 16:28
• In fact, I can replace the letters A-> Z by Greek letters. I'll look calmly at ArrayRules. Thank you @yohbs – SAC Jun 2 '17 at 16:48

In what you posted, I see no point in beginning with a sparse matrix. The resulting matrices will not be sparse. And since you want all of them, you want the biggest. And since all of them are submatrices of the biggest, we can just create the biggest and then extract the submatrices. So, if I have understood your request, you can use

bigmat[sqmat_, symbols_] :=
With[{d = Length[sqmat], slen = Length[symbols]},
Array[Function[{r, k}, If[r <= d && k <= d, sqmat[[r, k]],
If[r == k, 0, Subscript[symbols[[Max[r, k] - d]], r, k]]]],
{d + slen, d + slen}]]


to make the big matrix, and then use Part to pull out whichever of the submatrices you want. E.g.,

startMatrix = {{0, 1}, {0, 0}};
symbolSet = {mA, mB, mC};
bigmat[startMatrix, symbolSet][[;; #, ;; #]] & /@ Range[2, 5]


• Hi @Alan, How can I, instead of using mA, mB and mC, use a large list of random integers while maintaining the same logic? – SAC Jun 6 '17 at 15:20
• @SAC When you get an acceptable answer, it is courtesy to accept it. (Details: stackoverflow.com/help/someone-answers) When you have a separate question, etiquette says that it should be asked as a separate question. I would nevertheless try to answer your follow-on question here, except that I cannot understand it. How are you relating the symbolic matrix to these "random integers"? – Alan Jun 7 '17 at 13:58

I can write something similar using SparseArray

nmax = 5; (*number max of the dimension last matrix of the list*)
d = 2; (*dimension of the initial matrix*)
startMatrix = {{0, 1}, {0, 0}};(*initial matrix*)
q = Table[
RandomInteger[{1, i - 1}], {i, d + 1,
nmax}];(*List of Random Integer in interval [1,n-1], where n is a \
variable which characterize the dimension of the matrix *)
f[i_, j_] :=
If[i <= d && j <= d, startMatrix[[i, j]],
If[j > i, With[{s = q[[j - d]]}, Subscript[\[Xi], i, s]],
With[{p = q[[i - d]]}, Subscript[\[Xi], p,
j]]]](*The rule for construction of the matrix*)
m = Table[
SparseArray[{{i_, i_} -> 0, {i_, j_} -> f[i, j]}, {n, n}], {n, d,
nmax}];(*The list of the matrices*)
Table[MatrixForm[m[[i]]], {i,
4}](*See the Matrix Form of the list of matrices*)
`