5
$\begingroup$

Consider a table

Tabb = RandomReal[{0, 1}, {3, 2}];

I want to make the following table:

Tabb1=Flatten[Table[{Tabb[[i]][[1]],j,Tabb[[i]][[2]]},{i,1,Length[Tabb],1},{j,1,3,1}],{1,2}];

Is there a more elegant or faster way of making Tabb1?

$\endgroup$

5 Answers 5

12
$\begingroup$

Tabb has the structure:

{{a[1, 1], a[1, 2]}, {a[2, 1], a[2, 2]}, {a[3, 1], a[3, 2]}}

Now you want to introduce 1 in sublist 1. This can be done with "Riffle":

Riffle[{a[1, 1], a[1, 2]}, 1]

{a[1, 1], 1, a[1, 2]}

Then you want introduce 2 in sublist 2 and 3 in sublist 3. This is a job for "Outer:"

Flatten[Outer[Riffle[#1, #2] &, Tabb, {1, 2, 3}, 1], 1] ;

The result of this a list with depth 3, but you want a result of depth 2. We therefore need and additional "Flatten":

Flatten[Outer[Riffle[#1, #2] &, Tabb, {1, 2, 3}, 1], 1]

{{a[1, 1], 1, a[1, 2]}, {a[1, 1], 2, a[1, 2]}, {a[1, 1], 3, 
  a[1, 2]}, {a[2, 1], 1, a[2, 2]}, {a[2, 1], 2, a[2, 2]}, {a[2, 1], 3,
   a[2, 2]}, {a[3, 1], 1, a[3, 2]}, {a[3, 1], 2, a[3, 2]}, {a[3, 1], 
  3, a[3, 2]}} 
$\endgroup$
9
$\begingroup$
Tabb = Array[a, {3, 2}];

John Taylor (original)

t1 = RepeatedTiming[
   Tabb1 = Flatten[
      Table[{Tabb[[i]][[1]], j, Tabb[[i]][[2]]}, {i, 1, Length[Tabb], 1}, {j, 
        1, 3, 1}], {1, 2}];][[1]]

(* 0.0000139952 *)

Bob Hanlon

t2 = RepeatedTiming[
   Tabb2 = Flatten[Thread[{#[[1]], Range[3], #[[2]]}] & /@ Tabb, 1];][[1]]

(* 9.29298*10^-6 *)

Roman

t3 = RepeatedTiming[
   Tabb3 = Join @@ Outer[{#1[[1]], #2, #1[[2]]} &, Tabb, Range[3], 1];][[1]]

(* 0.0000116235 *)

Daniel Huber

t4 = RepeatedTiming[
   Tabb4 = Flatten[Outer[Riffle[#1, #2] &, Tabb, {1, 2, 3}, 1], 1];][[1]]

(* 7.44106*10^-6 *)

EDIT:

kglr

t5 = RepeatedTiming[Tabb5 = Riffle @@@ Tuples[{Tabb, Range[3]}];][[1]]

(* 7.01392*10^-6 *)

The methods provide equivalent results

Tabb1 === Tabb2 === Tabb3 === Tabb4 === Tabb5

(* True *)

kglr's is now the most efficient

{t1, t2, t3, t4, t5}/t5

(* {2.23145, 1.29783, 1.5505, 1.06513, 1.} *)
$\endgroup$
7
$\begingroup$
Tabb1 == Join @@ Outer[{#1[[1]], #2, #1[[2]]} &, Tabb, Range[3], 1]
(*    True    *)
$\endgroup$
1
  • $\begingroup$ I was just going to amend Tabb1 == Join @@ Outer[Riffle, Tabb, Range[3], 1] but @DanielHuber beat me to it. $\endgroup$
    – Roman
    Commented Apr 16, 2023 at 19:54
4
$\begingroup$
Riffle @@@ Tuples[{Tabb, Range[3]}]

enter image description here

Also

Map[Splice@Thread[{First@#, Range@3, Last@#}] &] @ Tabb 

Tabb1 == % == %%
True
$\endgroup$
0
$\begingroup$
Partition[
  Riffle[Flatten[ConstantArray[Tabb, 3], {{2, 1, 3}}], Range@3, {2, -2, 3}], 3]

:

Tabb = RandomReal[{0, 1}, {10000, 2}];
r1 = Map[Splice@Thread[{First@#, Range@3, Last@#}] &]@Tabb; // RepeatedTiming
r2 = Riffle @@@ Tuples[{Tabb, Range[3]}]; // RepeatedTiming
r3 = Partition[Riffle[Flatten[
       ConstantArray[Tabb, 3], {{2, 1, 3}}], Range@3, {2, -2, 3}], 3]; // RepeatedTiming
r1 === r2 === r3
{0.0192981, Null}
{0.0220029, Null}
{0.00404638, Null}
True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.