i'm trying to populate a sparse array with lists in certain positions, i tried the following code,

A = SparseArray[{i_, j_} /; 0 <= j - i <= 1 -> Avk, {10, 10 + 1}];

where Avk is 2x2 matrix, but mathematica yields that the assigned value must not be a list. i want the result to be like

enter image description here

where Avk and 0 are a 2x2 matrices (here is an example of dimension (3x4)), but the actual problem is (124x126) matrix.


closed as off-topic by Dr. belisarius, bbgodfrey, ciao, Jens, Kuba Apr 13 '15 at 6:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Dr. belisarius, bbgodfrey, ciao
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ As Mathematica tells you, this is not possible. Elements of a sparse array cannot be arrays themselves. It would be better if you asked about the problem you are actually trying to solve. $\endgroup$ – Szabolcs Apr 12 '15 at 16:27
  • $\begingroup$ Are you trying to make a four dimensional sparse array? That is not the same thing as a two-dimensional sparse array with array elements. $\endgroup$ – Szabolcs Apr 12 '15 at 16:40
  • $\begingroup$ @Szabolcs No it's just a 2 dimension matrix, as you can see in the image, it's a 3x4 matrix with elements are 2x2, in the end i need to do ArrayFlatten so this would be a 6x8 matrix ( i hope did well to describe my problem) $\endgroup$ – El Amraoui Abdelilah Apr 12 '15 at 16:47
  • $\begingroup$ You can use the answer to the following question, of which this is a possible duplicate: Entering block matrices, general case doesn't work $\endgroup$ – Jens Apr 13 '15 at 2:09
  • $\begingroup$ @Jens thanks man it was a great reference it worked as well $\endgroup$ – El Amraoui Abdelilah Apr 17 '15 at 1:35


... because my matrix is (214x216) so it's impossible to assign all these values one by one.

(* your 62  2X2 matrices av1 through av62 *)
avmat = Array[Subscript[Row[{av, #}], ##2] &, {62, 2, 2}]; 

sa = SparseArray[{Band[{1, 1}] -> avmat, Band[{1, 3}] -> avmat}];
(* {124, 126} *)
sa[[;; 20, ;; 20]] // MatrixForm (* display a portion of sa *)

enter image description here

Original post:

Perhaps you can use Band:

avm1 = Array[Subscript[av1, ##] &, {2, 2}];
avm2 = Array[Subscript[av2, ##] &, {2, 2}];
avm3 = Array[Subscript[av3, ##] &, {2, 2}];

sa = SparseArray[{Band[{1, 1}] -> {avm1, avm2, avm3}, 
                  Band[{1, 3}] -> {avm1, avm2, avm3}}];
sa // MatrixForm 

enter image description here


af = ArrayFlatten[{{avm1, avm1, 0, 0}, {0, avm2, avm2, 0}, {0, 0, avm3, avm3}}];
af // MatrixForm
(* same picture as above *)
  • $\begingroup$ yes man that works for the example, but the problem is that i need a dynamic code, because my matrix is (214x216) so it's impossible to assigne all these values one by one $\endgroup$ – El Amraoui Abdelilah Apr 12 '15 at 18:00
  • $\begingroup$ so k goes from 1 to 62? $\endgroup$ – kglr Apr 12 '15 at 18:09
  • $\begingroup$ yes you can say so, if i took k goes from 1 to 62, the finale matrix would be (124x126) since each element of the matrix is a (2x2) matrix $\endgroup$ – El Amraoui Abdelilah Apr 12 '15 at 18:11
  • $\begingroup$ it was helpful, $\endgroup$ – El Amraoui Abdelilah Apr 12 '15 at 19:10

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