Skip to main content
added 164 characters in body
Source Link
E. Chan-López
  • 33.7k
  • 3
  • 34
  • 54

Using MapThreadTranspose and InsertReplaceAll as follows:

a = {{1, 2}, {5, 2}, Transpose@{9, 3}a, {6,b[[;; 5}Length@a]]}; b = {x1, x2, x3, x4, x5,/. x6,x_ x7}/; MapThread[Insert[#1, #2, -1] &, {a,VectorQ[x] b[[;;:> Length@a]]}]Splice@x (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Or using Transpose and ReplaceAllCases as follows:

Transpose@Cases[Transpose@{a#, b[[;; Length@a]]Length@#]]}, /.{x x_: /;{__}, VectorQ[x]y_} :> {Splice@x, y}] &@a (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Using MapThread and Insert:

a = {{1, 2}, {5, 2}, {9, 3}, {6, 5}}; b = {x1, x2, x3, x4, x5, x6, x7}; MapThread[Insert[#1, #2, -1] &, {a, b[[;; Length@a]]}] (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Or using Transpose and ReplaceAll as follows:

Transpose@{a, b[[;; Length@a]]} /. x_ /; VectorQ[x] :> Splice@x (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Using Transpose and ReplaceAll as follows:

Transpose@{a, b[[;; Length@a]]} /. x_ /; VectorQ[x] :> Splice@x (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Or using Cases:

Cases[Transpose@{#, b[[;; Length@#]]}, {x : {__}, y_} :> {Splice@x, y}] &@a (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 
added 184 characters in body
Source Link
E. Chan-López
  • 33.7k
  • 3
  • 34
  • 54

Using MapThread and Insert:

a = {{1, 2}, {5, 2}, {9, 3}, {6, 5}}; b = {x1, x2, x3, x4, x5, x6, x7}; MapThread[Insert[#1, #2, -1] &, {a, b[[;; Length@a]]}] (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Or using Transpose and ReplaceAll as follows:

Transpose@{a, b[[;; Length@a]]} /. x_ /; VectorQ[x] :> Splice@x (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Using MapThread and Insert:

a = {{1, 2}, {5, 2}, {9, 3}, {6, 5}}; b = {x1, x2, x3, x4, x5, x6, x7}; MapThread[Insert[#1, #2, -1] &, {a, b[[;; Length@a]]}] (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Using MapThread and Insert:

a = {{1, 2}, {5, 2}, {9, 3}, {6, 5}}; b = {x1, x2, x3, x4, x5, x6, x7}; MapThread[Insert[#1, #2, -1] &, {a, b[[;; Length@a]]}] (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 

Or using Transpose and ReplaceAll as follows:

Transpose@{a, b[[;; Length@a]]} /. x_ /; VectorQ[x] :> Splice@x (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*) 
Source Link
E. Chan-López
  • 33.7k
  • 3
  • 34
  • 54

Using MapThread and Insert:

a = {{1, 2}, {5, 2}, {9, 3}, {6, 5}}; b = {x1, x2, x3, x4, x5, x6, x7}; MapThread[Insert[#1, #2, -1] &, {a, b[[;; Length@a]]}] (*{{1, 2, x1}, {5, 2, x2}, {9, 3, x3}, {6, 5, x4}}*)