Skip to main content
deleted 5 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[({#Grid[Prepend[Timing@ByteCount@#[A, Sequence @@ Timing @ ByteCount @ #[AB], B]}#] & /@   {f1, f2, f3, f4, f5, f6, f7, f8, f9}), Dividers -> All]All]~Style~24 //(* Style[#,thanks: 24]Mr.W &*) 

enter image description here

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[({#, Sequence @@ Timing @ ByteCount @ #[A, B]} & /@   {f1, f2, f3, f4, f5, f6, f7, f8, f9}), Dividers -> All] // Style[#, 24] & 

enter image description here

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[Prepend[Timing@ByteCount@#[A, B], #] & /@ {f1, f2, f3, f4, f5, f6, f7, f8, f9}, Dividers -> All]~Style~24 (* thanks: Mr.W *) 

enter image description here

deleted 21 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[({#, #[A, B] // ByteCountSequence //@@ Timing //@ SequenceByteCount @@@ ##[A, &B]} & /@ {f1, f2, f3, f4, f5, f6, f7, f8, f9}), Dividers -> All] // Style[#, 24] & 

enter image description here

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[({#, #[A, B] // ByteCount // Timing // Sequence @@ # &} & /@ {f1, f2, f3, f4, f5, f6, f7, f8, f9}), Dividers -> All] // Style[#, 24] & 

enter image description here

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[({#, Sequence @@ Timing @ ByteCount @ #[A, B]} & /@ {f1, f2, f3, f4, f5, f6, f7, f8, f9}), Dividers -> All] // Style[#, 24] & 

enter image description here

deleted 21 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[{#[[1]], #[[2, 1]], #[[2, 2]]} & /@ ({#, #[A, B] // ByteCount // Timing // Sequence @@ # &} & /@  {f1, f2, f3, f4, f5, f6, f7, f8, f9}),  Dividers -> All] // Style[#, 16]24] & 

enter image description here

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[{#[[1]], #[[2, 1]], #[[2, 2]]} & /@ ({#, #[A, B] // ByteCount // Timing} & /@ {f1, f2, f3, f4, f5, f6, f7, f8, f9}),  Dividers -> All] // Style[#, 16] & 

enter image description here

f = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; f[a, b] (* {{{1, 2, 0}, {3, 4, 0}}, {{5, 6, 1}, {7, 8, 1}}, {{9, 10, 2}, {11, 12, 2}}}*) 

All methods posted so far:

f1 = MapThread[Append, {#1, #2}, 2] &; f2 = Join[#1, Transpose[{#2}, {3, 1, 2}], 3] &; f3 = MapThread[{Sequence @@ #1, #2} &, {#1, #2}, 2] &; f4 = MapThread[Flatten[{##}] &, {#1, #2}, 2] &; f5 = MapThread[Insert[#1, #2, -1] &, {#1, #2}, 2] &; f6 = MapThread[Join[#1, {#2}] &, {#1, #2}, 2] &; f7 = Partition[MapThread[Append, {Flatten[#1, 1], #2 // Flatten}], 2] &; f8 = Join[#1, Map[List, #2, {-1}], 3] &; f9 = Module[{tmp = ConstantArray[0, Dimensions[#1] + {0, 0, 1}]}, tmp[[All, All, ;; -2]] = #1; tmp[[All, All, -1]] = #2; tmp] &; 

Timings and ByteCounts using Mr.W's test setup:

A = RandomInteger[99, {700, 400, 200}]; B = RandomInteger[99, {700, 400}]; Grid[({#, #[A, B] // ByteCount // Timing // Sequence @@ # &} & /@  {f1, f2, f3, f4, f5, f6, f7, f8, f9}), Dividers -> All] // Style[#, 24] & 

enter image description here

added 47 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
Loading
added 47 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
Loading
deleted 215 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
Loading
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959
Loading