Skip to main content
2 of 3
added 131 characters in body
kglr
  • 400.5k
  • 18
  • 488
  • 929

6.

ClearAll[sA]

sA = Module[{i$ = Complement[Range @ Length @ Join @ ##2, #]},
      SparseArray[{# -> #2, i$ -> #3}]] &;

Example:

s = {2, 3, 6}; a = {a1, a2, a3}; b = {b1, b2, b3, b4};

sA[s, a, b]

enter image description here

Normal @ sA[s, a, b]
 {b1, a1, a2, b2, b3, a3, b4}

7.

Normal[SparseArray[s -> a, {Length@Join[a, b]}]] /. 0 :> Last[b = RotateLeft@b]
 {b1, a1, a2, b2, b3, a3, b4}
kglr
  • 400.5k
  • 18
  • 488
  • 929