I use a loop to create a set of matrixes Subscript[k[x, y], i], depending on variables x, y. Then I create a large matrix

Kpr[x_, y_] = ConstantArray[0, {12, 12}];

In this matrix, using a loop, I write the values of matrices from the set in a similar way:

For[i = 1, i <= 12, i++, 
  Kpr[x, y][[1, 1]] = 
    Kpr[x, y][[1, 1]] + Subscript[k[x, y], i][[1, 1]]]`.

From this I get the message

Set::setps: Kpr[x,y] in the part assignment is not a symbol.

This means that no matrix elements are assigned.

If I set a matrix

Kpr = ConstantArray[0, {12, 12}]; 

without dependency on x and y and assign the elements of the matrix like so:

 For[i = 1, i <= 12, i++, 
   Kpr[[1, 1]] = Kpr[[1, 1]] + Subscript[k[x, y], i][[1, 1]]]`.

matrix formation will occurs. But it is important for me to get a matrix with a dependency on x and y.


For[i = 1, i <= 12, i++, 
  Subscript[k[x_, y_], i] = 
    {{x, y, 5 + i, 5}, {5, 4, 2 i, 0}, {x + i, 3, 5, y}, {4, 5, 3, 2}}]

Kpr[x_, y_] = ConstantArray[0, {12, 12}];
For[i = 1, i <= 12, i++, 
  Kpr[x, y][[1, 1]] = Kpr[x, y][[1, 1]] + Subscript[k[x, y], i][[1, 1]]]
  • 2
    $\begingroup$ I can't follow your code's logic unfortunately. Could you explain what you are trying to achieve?. $\endgroup$
    – MarcoB
    Commented Dec 5, 2020 at 20:01
  • $\begingroup$ Oh, sorry if you don't understand. I'm trying to build a large matrix from the elements of small matrices and keep the dependency on x,y. In fact, the elements of small matrices are distributed throughout the large matrix, if this is more clear. The problem is assigning an element of one matrix to an element of another matrix while preserving the representation of the matrix as a function. $\endgroup$
    – user76120
    Commented Dec 5, 2020 at 21:21

1 Answer 1


First, let me give you what I think is a piece of good advice. Subscripted variables may look pretty in a notebook but they can cause difficulties when you try use them in programs. You can take this as the wisdom of experience or see the note at the end of this answer to find out why this is so.

I will replace the subscripted variable k with the indexed variable k, which will be much more tractable. Like so:

Do[k[i][x_, y_] = 
  {{x, y, 5 + i, 5}, {5, 4, 2 i, 0}, {x + i, 3, 5, y}, {4, 5, 3, 2}}, {i, 12}];

This produces 12 functions, k[1], ..., k[12], which can be used to populate the diagonal of the 12 × 12 function Kpr.

Kpr[x_, y_] = ReplacePart[ConstantArray[0, {12, 12}], {i_, i_} :> k[i][x, y]];

This defines a Kpr matrix that returns a 4 × 4 k-matrix when queried on the diagonal and zero when queried off the diagonal. Examples:

Kpr[u, v][[3, 3]]
{{u, v, 8, 5}, {5, 4, 6, 0}, {3 + u, 3, 5, v}, {4, 5, 3, 2}}
Kpr[u, v][[3, 5]]

Note: When one uses subscripted variables, the values are stored as down-values of Subscript. Therefore, expressions like Clear using the variable's name have no effect on them. Indexed variables, however, are stored as sub-values of the named variable and things like Clear work as expected.

  • $\begingroup$ Thanks! You've been very helpful! $\endgroup$
    – user76120
    Commented Dec 7, 2020 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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