Skip to main content
edited body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

67.

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}

78.

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

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}

7.

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}

8.

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

5th way6.

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}

5th way

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}

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}
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

5th way

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}