2

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.

1 Answers1

2

Update:

... 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}];
Dimensions@sa
(* {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

Or

af = ArrayFlatten[{{avm1, avm1, 0, 0}, {0, avm2, avm2, 0}, {0, 0, avm3, avm3}}];
af // MatrixForm
(* same picture as above *)
kglr
  • 394,356
  • 18
  • 477
  • 896